• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain this output

 
jammy ponkia
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot Jesper
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • 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
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SriVidya, i dont think toString on string creates a new object.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks to all. I got it.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • 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: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic