naveen yadav wrote:
hers is my confusion
Is the memory reference location of str and ptr different ?
reason of doubt
If memory location of str and ptr is different , do there are two smiling objects in the heap ? shouldn't there be only one "Smiling" object in the string pool ?
please clear the confusion.
naveen yadav wrote:Is the memory reference location of str and ptr different ?
If memory location of str and ptr is different , do there are two smiling objects in the heap ?...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Actually, there are 3: two regular Strings, and one (the literal) in the pool.
naveen yadav wrote:
Winston Gutkowski wrote:
Actually, there are 3: two regular Strings, and one (the literal) in the pool.
2 regular string is fine . But how literal object is created ? i have not created it.
naveen yadav wrote:String str = new String("abc" );
Is that "abc" which is an argument represented by the string literal in pool ?
naveen yadav wrote:String str = new String("abc" );
Is that "abc" which is an argument represented by the string literal in pool ?
No, they do not refer to that String literal at all. They have their own data, separate from the literal, but identical.naveen yadav wrote: . . . since both have different memory location and both referring to the same string literal object .
naveen yadav wrote:ok .
but eventually there are only 3 objects created , 2 using new operator and one "abc" in string literal poll .
what does memory loaction of str and ptr represent ?
since both have different memory location and both referring to the same string literal object .
Jeff Verdegan wrote:You might think they refer to the same object because they get the same String literal passed to them, but they don't refer to that literal. That's just what's passed to their constructors. The c'tors make copies of that object, and its the copies that these variables refer to.
Martin Vajsar wrote:
Jeff Verdegan wrote:You might think they refer to the same object because they get the same String literal passed to them, but they don't refer to that literal. That's just what's passed to their constructors. The c'tors make copies of that object, and its the copies that these variables refer to.
That is not always true. Sorry for complicating things further, but since we're already discussing the internals of the String class, I'd like to point out that the String constructor reuses the original value array if the string passed to the c'tor is not a substring of the value array. Source code of the relevant String constructor in JDK5 is:
naveen yadav wrote:hi ranchers ,
hers is my confusion
Is the memory reference location of str and ptr different ?
reason of doubt
If memory location of str and ptr is different , do there are two smiling objects in the heap ? shouldn't there be only one "Smiling" object in the string pool ?
please clear the confusion.
Vinod Vijay Nair
Jeff Verdegan wrote:You're talking about the underlying array. I'm talking about the String objects themselves. I was trying to drive home the point that the new operator always creates a new object, and was making a guess at where Naveen's confusion might have been coming from.
Consider Paul's rocket mass heater. |