Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object Reference Equality

 
Binmei Zhou
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Byte b= new Byte ("127");
Why b.toString()==b.toString() is false
I need your help?
Thanks.
 
Pam Doucette
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Pam Doucette
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic