Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this false-ness happen?

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I have a question, this equality thing is driving me nuts....

Correct me If I'm wrong but in the following code I thought only one String Object(at the lined marked //3) would be created on the heap, Given that the Literal is the same, right?
Assuming that I further supposed that Both references, i and j referred the same String Object on the Heap....
So, my conclusion was that This little piece of code here Would Print true. But to my surprise... it outputs false



Could someone please give me all details of why This code outputs false?

My confusion is even greater than you imagine...because as you can see there's only one Integer Object instantiated. I thought that would play a role in this code's behavior, but it doesn't.

Best Regards,
Jose
[ April 09, 2008: Message edited by: Jose Campana ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the Integer class will create new Strings when it converts the integer into a String. New String, means different instances. So also both integers' value are equals, comparing their toString() with "==" will always be false.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Christophe,

That's what I supposed. That's the only explanation I could think of. good to know I wasn't as crazy as I thought.

so, toString() creates Strings as If the KeyWord new was used ?

Hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm, I'd like to know more.

Thanks !
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to know more.

The best thing to do in that case is to open up the source code for Integer.toString() and check it out for yourself:

So the characters that will represent the String are loaded into a char[], and that char[] is converted to a new String using a String constructor. No String literals are involved.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString() creates Strings as If the KeyWord new was used ?

Yes, Actually, not "as if". It really is. How would you expect it to convert an integer into a String without creating a new instance ?

I'd like to know more

Look into Integer.java sources. You'll see that "new String" is used in toString().
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doubt clarified !

Everything about this topic has been understood !

Thanks Java-Ranch dudes.

Java Ranch Rocks !
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[CV]: Yes, Actually, not "as if". It really is. How would you expect it to convert an integer into a String without creating a new instance ?

Well, it's possible to imagine an implementation that caches String objects for some commonly used numbers. Much like Integer.valueOf() caches Integer objects for ints in the range [-128, 127]. As it happens they don't do something like this in toString() - but they could. In general it's usually best not to assume anything about whether an object is a new object or a reused previously-existing object. Some methods may create new objects, and some may reuse them. Some may document which they do, and others may not. That's why we usually try to write code so it doesn't matter whether you get a new object or not - e.g. use equals() rather than == in most cases.
[ April 09, 2008: Message edited by: Jim Yingst ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. The implementation could differ depending on the compiler provider (Sun, Microsoft...) so I should not have assumed it to work this way. Thank you for pointing it out.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christophe Verre:
The implementation could differ depending on the compiler provider


Doesn't have anything to do with the compiler, but with the runtime environment used to execute the code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic