I had posted some String trivia questions for a friend of mine and he responded with this one, which got me. I thought it was excellent to know because I would *think* this type of question could be on the exam. I will purposely insert a number of blank lines to give you time to think of the answers before reading them.
Ready? Here they are: false true false false false false
Yep, the only one that has a matching reference is (a==c). Now, the big question is why? Here's my interpretation, but I'm not certain that it is 100% correct. First, note that if String d had said: String d = new String("AB"); then the answers would still have been exactly the same. Now remember that a String object lives on the heap, but the value lives in the StringPool (an area of memory that I'm guessing lives on the heap???) Thus, when String c is created, it looks at the StringPool, says, "oh, here is a string called AB, and a String object that refers to it. So it gives c the reference to the String object that a refers to. Now, I'm guessing that in the case of String b and String d (and I'm ignoring the strings that get created in the construction that are discarded..."A" and "B"), it is told to create a new String object on the heap, which would thus have a different reference for each newly created String object on the heap. Subsequently, it looks in the StringPool and says, "Ah ha! There is an AB in here!" So it gives the String object the reference to AB in the StringPool (which would presumably then be the same reference to AB in the String object for String a and String c). However, the String object itself---not its value---had a new instance created and thus has a different reference. This would explain the results above, but my visual representation could be way off since I assume (1) the StringPool lives in the heap and (2) that the String object contains a reference to an object in the StringPool. I want to ensure I'm understanding or interpreting this correctly. Thoughts? Comments? Ross
String a = "AB"; String b = new String ("AB"); String c = "A" + "B"; String d = new String("A" + "B"); In the above the object references b & d are different and so they cannot be same even though the contents are same. ( == will fail)I understand this concept but how come 'a' reference compared with 'c' gives true. Does the compiler convert the statement as below. String c = "A" + "B";// ==> String c = "AB"; Regards Vikram
The expression "A" + "B" is invariant, so the compiler will concat and get "AB", which already exists in the string pool. Todd Killingsworth
posted 17 years ago
Remember that strings are immutable (unchangeable). So when you say something like: String x = ("A" + "B") you are actually... 1. Creating a StringPool object called "A" and a String object pointing to it. (NOTE: I'm assuming the String object actually refers to the StringPool object "A" as opposed to simply having the value "A" which would then be an instance variable (state) of the String object). I don't know that there is actually a StringPool object, but I do know there is shared memory for strings...I am assuming that is an object on the heap called StringPool so we can refer to it, but this assumption may be faulty (my disclaimer!) 2. Creating a StringPool object called "B" and a String object pointing to it. 3. Creating a StringPool object called "AB" and assigning it to String x. Note that now we have abandoned two string objects (the one pointing to StringPool object "A" and the one pointing to StringPool object "B"). I don't think the StringPool is ever cleared out, but again, I'm not sure...so those three strings may exist in it for the life of the program's execution. Thus, when you create another string that ultimately refers to any string object that ultimately contains "AB", it will return the reference to the existing one. But---and this is the assumption I'm making---when you refer to one that makes a new String, you're getting a new reference to a brand new String object that also points (refers) to the text "AB" in the StringPool. Of course, one or more of my assumptions above may be wrong, but if they're correct, it would explain the behavior. Ross
I think the answer to this is simpler than it looks. I will expand on what has been already said. When we create a string with: String s1 = "AB"; We create one String object that is placed in the string pool and the variable s1 reference address will lead you straight to the string pool. Expl: String s1 = "AB"; // and String s2 = "A" + "B"; "AB" already in the pool, s2 will refer to the same address in the pool. On the other hand when we create a string with: String s3 = new String ("AB"); We create one string object placed in the pool, and one in the non-pool which s3 will reference. Bottom line: When second String s1 = �AB�; is created reference s1 points to the same string in the string pool. When String s2 = new String(�AB�); is created �s2� points to the new separate object (non-pool) cheers
[ July 23, 2003: Message edited by: Alex Radomski ]
posted 17 years ago
Yep, I see that now in re-reading K & B's p. 360 (after what you said)....but according to the text, it does still add the literal "AB" into the pool (if it doesn't exist, presumably). Thus, if it does exist, s2 wound refer to the new String object in the non-pool (heap) memory, but the String literal "AB" is still in the pool (or perhaps referred to by the String object s2 refers to). As the text explains: String s = "abc"; //creates one String obj and one reference var In this case, it says "abc" will go to the pool and s will refer to it. String s = new String("abc"); // creates two objs and one ref var In this case, it says "abc" is added to the pool (presumably if it doesn't exist) but that s refers to the new String object that is not in the pool---this implies that the String object it refers to in the heap refers to the "abc" literal in the pool. Ross
Stop it! You're embarassing me! And you are embarrassing this tiny ad!