wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How does this false-ness happen? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does this false-ness happen?" Watch "How does this false-ness happen?" New topic
Author

How does this false-ness happen?

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: May 28, 2007
Posts: 339
Doubt clarified !

Everything about this topic has been understood !

Thanks Java-Ranch dudes.

Java Ranch Rocks !
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
GeeCON Prague 2014
 
subject: How does this false-ness happen?