File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes case of two equal object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "case of two equal object" Watch "case of two equal object" New topic
Author

case of two equal object

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Oct 23, 2011
Posts: 384

String str = new String("abc" );

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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 23, 2011
Posts: 384

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 .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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 Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Oct 23, 2011
Posts: 384

thanks every body for sharing your view of the topic and clearing my doubts.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

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.


Vinod Vijay Nair
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: case of two equal object