• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

toString() of wrapper classes.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

If I run the following sample code in main method, It is printing "false".

Byte myByte=new Byte("111");
if(myByte.toString() == myByte.toString()){
System.out.println("True");
}else {
System.out.println("false");
}

If I change the above code as follows (creating byte with 10),

Byte myByte=new Byte("10");
if(myByte.toString() == myByte.toString()){
System.out.println("True");
}else {
System.out.println("false");
}
Then it is printing "True".

Anybody please explain the concept behind this.
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember this topic coming up here before... but I don't know the answer.

Hmmmm... Let's look at the source code...

It looks like the byte.toString() method calls the String.toString(int) method, which makes a call to Integer.valueOf(int, radix), which...

Well, it looks like for reasons that I don't know why, they make a special case for -3 to 10. There is a switch statement that return fix strings for this range.

I guess that explain why it happens. It doesn't explain why they did it that way though...

Henry
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Roja Rani:
]Hi All,

If I run the following sample code in main method, It is printing "false".

Byte myByte=new Byte("111");
if(myByte.toString() == myByte.toString()){
System.out.println("True");
}else {
System.out.println("false");
}

If I change the above code as follows (creating byte with 10),

Byte myByte=new Byte("10");
if(myByte.toString() == myByte.toString()){
System.out.println("True");
}else {
System.out.println("false");
}
Then it is printing "True".

Anybody please explain the concept behind this.




Hi Roja,
Are you sure the second code prints True....??

Well For me It never printed true...Infact it will always print false because toString() always returns a new String object.

Correct Me if I am wrong
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prints false in both cases
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString return new String
then it must not same string
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prints false in boths cases, which is the expected answer. (1.5.0)
Maybe you had mybyte.toString().equals(mybyte.toString()) in mind? IN which case, it should return falsein both cases.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Prints false in both cases




I am using jdk1.4.2_04 and the program gives me the output...

False
True


Reason..am looking forward ..
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am using jdk1.5.0_05 and the output i obtained is

False
False

which seem to be logically right!!!

Regards
Simon
 
Henry Wong
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The source code that I traversed was for 1.4.2. Personally, I think it was silly that they made a special case for numbers -3 to 10. In any case, as others have reported, they fixed it in 1.5.

Henry
 
Roja Rani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thank you for the reply. I understood that in jdk 1.4, there is using special case for -3 to 10 while converting to toString() of wrapper classes and that special case is not there in Jdk 1.5.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by A Kumar:
Hi,



I am using jdk1.4.2_04 and the program gives me the output...

False
True


Reason..am looking forward ..




But when i compiled the code in JDK 1.5 with the -source 1.4 flag...
it still gave me the same result
false
false
???
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



quote:
Originally posted by A Kumar:
Hi,



I am using jdk1.4.2_04 and the program gives me the output...

False
True


Reason..am looking forward ..



But when i compiled the code in JDK 1.5 with the -source 1.4 flag...
it still gave me the same result
false
false





No idea...
 
Henry Wong
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sandeep Chhabra:

But when i compiled the code in JDK 1.5 with the -source 1.4 flag...
it still gave me the same result
false
false
???



It is not the compiler, it is the library.

Here is the offending method. It is from the Integer class -- which eventually gets calls by the Byte class. This source is for JDK 1.4.2_08.



Henry
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very confusing in case of == ..

i have tried using jdk1.4 it prints true for values -3 to -10 as said by my collegues..whereas for others it prints false as expected.

thanks

sri
 
Don't play dumb with me! But you can try this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic