This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Adding to that if someone could throw some light upon as to how many objects are actually created by this line
String s2 = new String("123");
I guess it should be two. One in the literal pool (123) and the other one created using new operator.
I believe only one object will be created and that too not in pool.
String str = "ONE"; .... This will create in pool .. when you use new operator, it will not be created in pool.
Joined: Oct 04, 2004
You are right that String s = "dasdsa"; will create only one object and that too in pool bu with String s1 = new String("dasda"); will create one object in non-pool and the one reference in pool which will refer to non-pool object.
But I think we are diverting from the actual question that I have put. If anybody can explain me when these will be garbase collected, I will be thankful.
String s = "dasdsa"; Will only create a new object in the String pool if no other Object in the String pool currently exists with that sequence of characters. The real exam does not focus on String pool issues with regard to Garbage collection or numbers of objects created, as it is somewhat of an exception to the general rule on object creation.