Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Kumar #9

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is the question:
Byte b1 = new Byte("127");
if (b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");
A. compilation error, toString() is not avialable for Byte.
B. Prints "True".
C. Prints "False".
the answer is C. "Flase".
I don't understand. Since both side used b1, how come b1.String is not the same?
Thanks in advance.
LeClair
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toString() when applied to byte
Returns a new String object representing the specified Byte.
So when u specify b1.toString() twice it creates two new string objects,so thats why you can't use the == operator here
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leclair
I think the explanation is quite simple.
b1.toString() returns a 'new String Object' and not a String literal. So the comparison is between two reference variables of 2 different String Objects havin the same 'Value'
Hence straight == comparison results in false.
Its like doing,
String s1 = new String("127");
String s2 = new String("127");
here s1!=s2,
but
s1.equals(s2) OR s1.compareTo(s2)==0
Bye.
Take Care
Ashish
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the explaination. Then, am I right that, there is no way for "==" to return true when either side is using toString method.
Indy
 
Ashish Thakur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats correct!
Good GOing, when are u giving the test

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much, Ashish, for clearing that for me.
I am going to take the exam within this week.
Leclair
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic