Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on == in comparing String and non-String objects using the toString() method

 
Johan Pelgrim
Ranch Hand
Posts: 105
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please look at this piece of code:

This will give output

Is it true that, in case of the Byte object you�re comparing references to two 'newly' created String objects (thus false) and in case of the String object you�re (somehow) comparing references to existing Strings objects? So what does this mean? That the toString() method for the String class simply returns a reference to the already existing String object?

Thanks for any further explanation on this.

Johan Pelgrim
The Netherlands
[ May 26, 2005: Message edited by: Johan Pelgrim ]
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the javadoc, myByte.toString behaves exactly like Byte.toString(byte) method which:

Returns a new String object representing the specified byte. The radix is assumed to be 10.


The operative word here would be "new". I don't have access to the source at the moment, so I only have their sun's word here, but that would explain why you got false before.


Also, according to the javadoc for String, description of the toString() method:

This object (which is already a string!) is itself returned.


So "this object" instead of a new reference to the String
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the JSDK Documentation of String.toString() and Byte.toString() this is what happens:

String.toString() returns itself, that's to say a new String is not created. That is why String.toString()==String.toString() will evaluate to true.

While Byte.toString() will actually return what the static method String.toString(byte) returns, that's to say a new String and hence Byte.toString()==Byte.toString() will evaluate to false.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have not seen any static method in javadocs of sun

i.e static method String.toString(byte)

if i m wrong pls correct me...and

so final conclsion is that .... String.tostring(byte) method will always create new string object on heap ..thats why both string object returning false on == test ??

pls give last confirmation of this post...

regards
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amit taneja:
i have not seen any static method in javadocs of sun

i.e static method String.toString(byte)

I think he meant String.valueOf(byte)

so final conclsion is that .... String.tostring(byte) method will always create new string object on heap ..thats why both string object returning false on == test ??

pls give last confirmation of this post...

regards


Not really. There are cases that it will return the same object, however this is an implimentation detail and is subject to change. There is no garuntee that the Strings returned will be the same or different objects.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what will we do if we get such question in exam

and what about increaseing knowlege about this tricks...

is we have to do research for that.....
i mean ..there is no std as u say that it changes from case to case..
 
Johan Pelgrim
Ranch Hand
Posts: 105
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thanks for your thoughts. I think I'll go with Timmy Marks' reply that the toString() method for the Byte object returns a new String object, where the toString() method for a String object returns a reference to itself (If it's intermittent I'll go mad )

Thanks again...

Preparing to take the test on Monday or Tuesday ;-)

Johan Pelgrim
The Netherlands
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he meant String.valueOf(byte)


I think he meant Byte.toString(byte) since that's what it says in the javadoc (look at the toString() method. There is a link to Byte.toString(byte) which says explicitly that a new String object is returned.

P.S. Johan, good luck on the exam!
[ May 26, 2005: Message edited by: Timmy Marks ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic