aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain this output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain this output" Watch "Please explain this output" New topic
Author

Please explain this output

jammy ponkia
Greenhorn

Joined: Mar 23, 2008
Posts: 15
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

Joined: Aug 16, 2005
Posts: 14274
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
jammy ponkia
Greenhorn

Joined: Mar 23, 2008
Posts: 15
Thanks a lot Jesper
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
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

Joined: Jan 16, 2008
Posts: 162
Hi SriVidya, i dont think toString on string creates a new object.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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

Joined: Feb 17, 2008
Posts: 166
Thanks to all. I got it.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
// 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

Joined: Dec 06, 2005
Posts: 1321

please read my last post.

or do change line 1 with this code.

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
Thanks mehmood, but I did not ask why its giving a compiler error did i?
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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

Joined: Dec 06, 2005
Posts: 1321

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

Joined: Sep 17, 2006
Posts: 1282
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.
 
 
subject: Please explain this output