This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
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
posted
0
Hi SriVidya, i dont think toString on string creates a new object.
Nicholas Jordan
Ranch Hand
Joined: Sep 17, 2006
Posts: 1282
posted
0
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.
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.
// 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!
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";