Hi All, Pls look at fallowing question, How many Strings are in the VM string-pool after executing this code fragment ? String s1 = "javacoding.net" String s2 = "javacoding.net" String s3 = new String("javacoding.net") Ans given is 2. Can anyone explain me.? What i thought was unless s3.intern(); is called it will not be taken into string-pool, am i right?.So ans should be 1. Thanks.
s1 and s2 both point to the one String in the pool .. agreed. next bit: your code is - String s3 = new String("javacoding.net") which creates a new String instance in the program space and one in the Pool. but if you went String s3 = new String(); you would have two references to the object in the program space, but only one object in the Pool. oh, i am not very sure on this myself, i hope someone else can say some intelligent stuff!
Joined: Dec 04, 2002
after executing String s3 = new String("javacoding.net") , will it create object in string pool apart from program space? Then what intern(); does.?
I also think that there will only one object in the String pool and total two object will be there one in non-pool memory and one in the pool. 3nd line should not create any object in the pool as the same object exists there alreay. Even if you call s3.intern() after , that also will not create a new string in the pool for the same reason. It will just point to the existing the object in the pool. So (s2==s3.intern()) returns true. You can test it. The API documentation says "When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. " In my opinion creating same object in the string pool more than once violates the concept of the string pool. I donot know what expert says about it.
Dude..... new String("ABC") creating two objects is still a mystery to me one in the main memory and one in the pool. here are contradictory statements regarding String.intern()/new String("ABC"): (lets assume, S=new String("ABC"); is the only statement in the main method). 1. API states
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
For the exam, you need to know that IF you use 'new' to create a new String that is not *already* in the pool, TWO String objects are created. But there is no need to ever have duplicates in the String pool, so if the String is already in the pool at the time you use 'new', there will be only ONE created.
If this true, Prashanth and others, I agree answer to the original post would be 1(one). WHY WOULD I EVER NEED TO INTERN A STRING??? just to get a reference from the pool??? Why would API state "Otherwise, this String object is added to the pool and a reference to this String object is returned" ? The string would already be in the pool!!! -Shiva PS: JavaRanch webmaster, Can you please make the TextArea for Message field wider in post reply/edit post pages? It is hard to review a message in small TextArea!!! [ January 28, 2003: Message edited by: Shiva Mantri ]
Howdy -- I'm probably just going to make this *more* confusing... but I'll give it a try anyway ; ) First, I want to be clear that a deep understanding of the String pool is NOT needed on the exam. I see many questions in this forum that are well beyond the scope of what is useful to know... or even beyond what IS knowable according to the spec. I believe that the answer to that question is indeed '1', but only because it said "How many will be in the String pool..." If it had been changed to, "How many OBJECTS are created?" (regardless of whether those objects are part of String pool or not) would be 2. Imagine you say: String x = "foo"; if the literal "foo" is already in the pool, no new objects are created. If the literal "foo" is NOT in the pool, one new object is created. Imagine you say: String y = new String("foo"); Since one is IN the pool, only ONE new object is created. So now ONE is in the pool, and ONE is referenced by 'y'. If "foo" had not previously been made and put into the pool, then TWO objects would be created, one String object with "foo" that goes into the pool, and one new object with "foo" that is referenced by the variable 'y'. If you say y.intern(), then y now refers to the one in the pool, rather than the other one (which is now abandoned). And while we're here... String a = "foo"; String b = "boo"; String c = a + b + "moo"; We can assume that there are now FOUR objects. "foo", "boo", "moo", and the one referenced by c, "fooboomoo". What we do NOT know for certain is whether you will also get a "fooboo" created by the concatenation of a + b. That will most likely be optimized away using a StringBuffer, so we can NOT say that intermediate Strings created by concatenation are all turned into individual String objects. And if you had said: String a = "foo"; String b = "boo"; String d = "moo"; String c = a + b + d; Again, four objects are all we know for certain. I guess the rule is: * If you see a literal, it will be placed in the pool if not there already. String a = "gee"; // puts "gee" in pool if not there * If you see a new String("gee"); you know that either ONE or TWO objects are created, depending on whether "gee" is already in the pool. * If you see a concatenation of variables, you do NOT know whether a new String object is created for the intermediate concatenations, so String s = a + b + c + "goop"; The only thing you can say is that probably one or two objects are generated, depending on whether "goop" is in the pool already. In fact, if both "goop" and the result of the expression are both in the pool, then NO new objects need be created. That does not mean that there will NOT be muliple objects created, merely that you will not know for certain. But that is NOT on the exam! You ARE expected to know, however, that: String a = "goo"; String b = a + "foo"; assuming no other objects have previously been created, will make 3 objects: "goo", "foo", "goofoo". Even this is a very minor part of the exam. I would NOT WORRY much about the String pool beyond knowing that: * literals go in the String pool, if not there already * new String ("foo") causes either one or two objects to be created. One object if "foo" is already in the pool, and TWO objects if "foo" is not already in the pool (so it would make one for the pool and a duplicate but separate String object for the new. It really is not a big deal. It is FAR more important to understand the immutability of Strings. You WILL have questions about that! Although those are easy, because as long as you remember that no matter what you do, you NEVER alter an existing String, you're in good shape. String s = "foo"; s.concat(g); s.toUpperCase(); You need to know that the value of 's' is still "foo", and that the other two objects created by the concat() and toUpperCase() have both been abandoned since they were not assigned to anything. Yikes! OK, summary for the exam: 1) Know that Strings are immutable 2) Know that literals go in the pool unless there already 3) Know that new String(...) creates a new String object even if a duplicate already exists in the pool. I think that's it : ) cheers, Kathy