• 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

Please explain this output

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can you explain me the below code:

Byte b1 = new Byte("127");

if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");

Why does this print false?

Thanks,
Jaimesh.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because in this line:

if(b1.toString() == b1.toString())

you are comparing two String objects with the == operator, and that will only return true if the two arguuments on both sides of the == refer to the exact same String object.

== compares references, not values.
 
jammy ponkia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Jesper
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jesper

Does toString() creates a new String object each time when we give b1.toString(). To my understanding

"127".toString()=="127".toString(); returns true.

then why not b1.toString()==b1.toString() return true.

Correct me where I went wrong.

Thanks
Srividhya
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi SriVidya, i dont think toString on string creates a new object.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sridhar row:
Hi SriVidya, i dont think toString on string creates a new object.



Correct:
Several issues seem to be overlooked here. First, the orginal poster declares a byte then uses a string initializer. I would think the compiler would at least issue a warnning.

 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. I got it.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Jordan:
the orginal poster declares a byte then uses a string initializer. I would think the compiler would at least issue a warnning.



It won't. because there is two way specified in Java Docs to initialized Byte Object.

Byte Class Constructor Summary
Byte(byte value)
Constructs a newly allocated Byte object that represents the specified byte value.
Byte(String s)
Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

// Try this.
Byte b1 = new Byte(127);
String one = b1.toString();
String two = b1.toString();
int difference = one.compareTo(two);//
System.out.println(Integer.toString(difference));



above code gives a compiler error:
cannot find symbol
symbol : constructor Byte(int)
location: class java.lang.Byte
Byte b1 = new Byte(127);

Also, when you compare one and two using == it should give false as they are two different objects from what i have understood. Please correct me if i'm wrong.Thanks!
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please read my last post.

or do change line 1 with this code.

 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mehmood, but I did not ask why its giving a compiler error did i?
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sridhar row:
when you compare one and two using == it should give false as they are two different objects from what i have understood.



Yes, They are two different objects, because of that it print "false";
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sridhar row:
but I did not ask why its giving a compiler error did i?



of course you didn't, but posting the code with error is equal to asking.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I'll take the fall on this - it is worth it if the orginal poster got his question answered. Let us know Jammie if you need more.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic