How many objects are create when this code will execute...
String string = new String("Java is best Lang.");
tell me the number of object of string which will create . All are those are eligible for garbage collector + not eligible both.
Please Reply ASAP
Thank you Ashvin Jain
Whenever we create any class object by using new keykord , the jvm allocate the memory for that objects in jvm's heap. The garbage collection only take the responsible to remove the unused objects from head memory.
ex: String s=new String("java is a best Lang");
In the above example the String object is created in heap memory.In this above String object is not associated with any reference variable the garbage collection take the responsible to remove this object
s=null; // Heare we are making the above object
// eleible for garbage collection
Whenever the boxing is done or whenever we assign the string value to String class variable directly by using assignment operator ,the jvm allocate the memory for those object is constant pool. Here the garbage collection does not take the responsiblt to remove unused objects from constant pool. Internally the jvm take the responsiblt to remove the objects form constant pool if required.
ex: String s1="java is best Lang";
Hear the String object is created in constant pool.
s1=null; In this senario the garbage collection does not take the responsible to remove these types of object.
Hence String s=new String("java is best Lang");
The above line only creat one object that is also in heap memory.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.