• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

case of two equal object

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Yes. The memory reference location of str and ptr are different. Yes. There are many smiling string objects on the heap. And yes. Only one smiling string object is referenced from the string pool -- and interestingly, it is neither of the two objects referenced by str or ptr.

Henry

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote:Is the memory reference location of str and ptr different ?


Yes.

If memory location of str and ptr is different , do there are two smiling objects in the heap ?...


Actually, there are 3: two regular Strings, and one (the literal) in the pool.

However, all this is basically of very little practical use, except as a cautionary tale about using new String() with a literal.

Far better is to remember never to use '==' with objects. Always use .equals().

Winston
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Actually, you have, by using the string literal. The compiler does a lot of stuff for you, and one of them is to create strings used by the class -- and insert them into the string pool.

Henry
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str = new String("abc" );

Is that "abc" which is an argument represented by the string literal in pool ?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote:String str = new String("abc" );

Is that "abc" which is an argument represented by the string literal in pool ?


Yes.

Generally speaking, everything that appears as a String constant (ie. text enclosed in double quotes) is a String literal and resides in the string pool. The compiler does some optimization though. For example:
In this code the compiler recognizes that the expression assigned to the LONG_TEXT variable is a constant String expresion, and creates one String literal in the string pool with the value of This is a very long String literal. To improve readability, it was broken in two lines.

Details are described in the JLS.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote:String str = new String("abc" );

Is that "abc" which is an argument represented by the string literal in pool ?



Yes. When your class is loaded, every String literal in your code is put into the String pool (if there's not already an equal String there). So before you even reach that line of code, the String "abc" exists in the pool.

When you use the new operator, it always creates a new object--Strings are no different in this respect. So, now you have created a new String object in the "normal" part of the heap that is a copy of the "abc" String object from the pool.
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote: . . . since both have different memory location and both referring to the same string literal object .

No, they do not refer to that String literal at all. They have their own data, separate from the literal, but identical.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveen yadav wrote:ok .



but eventually there are only 3 objects created , 2 using new operator and one "abc" in string literal poll .



Only 3 String objects. There are also the char[] objects that the Strings use to hold their characters.

what does memory loaction of str and ptr represent ?



That depends what you mean by "memory location of str and ptr".

They are two different variables, so they are the names of two different locations in memory. For instance, "str" might get compiled into "stack pointer + 4" and "ptr" might get compiled into "stack pointer + 8".

In addition to that, they each hold a value, which is a reference to an object. You can think of that as the "memory address" of the object, but that's not how the spec defines it, and that's not what it's required to be. The values stored in location "str" and location "ptr" might be the same (meaning they both point to the same object) or they might be different (meaning they point to different objects). In this case, they're different--the variables refer to two different objects.

since both have different memory location and both referring to the same string literal object .



No, they don't both refer to the same object. We can tell this because they both refer to a result from the new operator, which always creates a new object.

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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks every body for sharing your view of the topic and clearing my doubts.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:



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.

So, as I stated, new will create a new String object, and even in the case of substring you're talking about, however, a new String object is created.
 
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Two reference variables pointing to two different objects and one literal(smiling) on java heap.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Yep. Sorry, I should have paid more attention.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic