wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object Reference Equality 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 "object Reference Equality" Watch "object Reference Equality" New topic
Author

object Reference Equality

Binmei Zhou
Greenhorn

Joined: Mar 18, 2001
Posts: 3
Hello,
Byte b= new Byte ("127");
Why b.toString()==b.toString() is false
I need your help?
Thanks.
Pam Doucette
Greenhorn

Joined: Mar 13, 2001
Posts: 27
I think it is because each b.toString() returns a separate String object, each having a different area in memory. Consquently the == will return false, although .equals() returns true
public class ByteCheck
{
public static void main (String args[])
{
Byte b= new Byte ("127");
System.out.println("byte to string == is " + (b.toString()==b.toString()));
System.out.println("byte to string equals() is " + b.toString().equals(b.toString()));
}
}
Binmei Zhou
Greenhorn

Joined: Mar 18, 2001
Posts: 3
Hi Moose,
Thank you very much for your prompt reply.
How about
Boolean b= new Boolean ("False");
b.toString==b.toString is ture.
however
String s1=new String ("False");
String s2=new String ("False");
s1==s2 is false ;
Why?
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
I think it will return true in case of Boolean and Strings; for all other cases it will be false.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The toString method of class String returns the object that is fed in.
String s1=new String ("False"); //creates an object on the heap
String s2=new String ("False"); //creates a different object on the heap
s1==s2 is false ; //those are two different objects with different references.
The toString method of class Boolean returns a string LITERAL representation of the value.
Boolean b= new Boolean ("false");
b.toString==b.toString is true.
The "false" string literal is resolved at compile time and resides in the Constant Pool. Each of the b.toStrings points at the SAME string literal and the equivalency test is therefore true.
[This message has been edited by Cindy Glass (edited March 19, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Pam Doucette
Greenhorn

Joined: Mar 13, 2001
Posts: 27
Thanks for a definitive answer Cindy. Other examples include:
(These print false)
public class CheckBoo
{
public static void main (String args[])
{
Integer one = new Integer("1");
System.out.println(one.toString()==one.toString());
Double two = new Double(2.0);
System.out.println(two.toString()==two.toString());


}
}
Binmei Zhou
Greenhorn

Joined: Mar 18, 2001
Posts: 3
Thanks Cindy.
I am still confused.
Byte b=new Byte("127");
b.toString()=127;
which return a String object representing the primitive value, should be the same. Just like following:
String s1="Hello";
String s1="Hello";
s1==s2 is true, since the s1,s2 denote the same String object.
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
<pre>
hi Binmei,
reading through this posting ... i feel it is gettina little confusing .... so hoping to settle the confusion i have written down a small code with which i hope we can pusue the doubt.

class Check
{
public static void main(String[] arg)
{
Byte b= new Byte ("12");
Boolean bval= new Boolean ("true");
String s1=new String ("False");
System.out.println("--------------------------");
System.out.println("byte == returns " + (b.toString() ==b.toString())); //false
System.out.println("boolean == returns " + (bval.toString()==bval.toString())); //true
System.out.println("String == returns " + (s1.toString()==s1.toString())); //true
System.out.println("--------------------------");
System.out.println("byte tostring equals() is " + b.toString().equals(b.toString())); //true
System.out.println("boolean tostring equals() is " + bval.toString().equals(bval.toString())); //true
System.out.println("String toString equals() is " + (s1.toString().equals(s1.toString()))); //true
System.out.println("----------------------------");
System.out.println(b==b); //true
System.out.println(bval==bval); //true
System.out.println(s1==s1); //true
System.out.println("-----------------------------");
}
}
</pre>
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
<pre>
hi Binmei,
reading through this posting ... i feel it is gettina little confusing .... so hoping to settle the confusion i have written down a small code with which i hope we can pusue the doubt.

class Check
{
public static void main(String[] arg)
{
Byte b= new Byte ("12");
Boolean bval= new Boolean ("true");
String s1=new String ("False");
System.out.println("--------------------------");
System.out.println("byte == returns " + (b.toString() ==b.toString())); //false
System.out.println("boolean == returns " + (bval.toString()==bval.toString())); //true
System.out.println("String == returns " + (s1.toString()==s1.toString())); //true
System.out.println("--------------------------");
System.out.println("byte tostring equals() is " + b.toString().equals(b.toString())); //true
System.out.println("boolean tostring equals() is " + bval.toString().equals(bval.toString())); //true
System.out.println("String toString equals() is " + (s1.toString().equals(s1.toString()))); //true
System.out.println("----------------------------");
System.out.println(b==b); //true
System.out.println(bval==bval); //true
System.out.println(s1==s1); //true
System.out.println("-----------------------------");
}
}
</pre>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object Reference Equality
 
Similar Threads
String doubt
==, why is it so ?
== comparison
Can a Byte be converted to String?
toString() returns String ?