Forums Register Login

Doubt on "=="

+Pie Number of slices to send: Send
From the following code why the out put is 'False'.Even though the values are same for the object b1 and b2.


Byte b1= new Byte("127");
Byte b2 = new Byte("127");
System.out.println(b1.toString());
System.out.println(b2.toString());
if(b1.toString() == b2.toString())
{
System.out.println("true");
}
else
{
System.out.println("false");

}
thanks in advance
reena
+Pie Number of slices to send: Send
 

Originally posted by sourav nayak:
From the following code why the out put is 'False'.Even though the values are same for the object b1 and b2.


Byte b1= new Byte("127");
Byte b2 = new Byte("127");
System.out.println(b1.toString());
System.out.println(b2.toString());
if(b1.toString() == b2.toString())
{
System.out.println("true");
}
else
{
System.out.println("false");

}
thanks in advance
reena



Because

and


both are creating different String objects.

And when String object are different then it should be compared with


method.

Hope it clear you.
+Pie Number of slices to send: Send
Hello,

The == will compare just the bit pattern of address ( some way to get the object), so the false will be printed.

See,

if("Hello".equals("Hello")) sop("true); // ok
if("Hello" == "Hello")System.out.println("Hello"); // ok

but

Byte byte1=new Byte((byte) 23);
Byte byte2=new Byte((byte) 23);
if(byte1.toString() == byte2.toString())
System.out.println("OK");

Thing is the toString() method will not create the String in StringPool but in Stack that to temporary purpose.
+Pie Number of slices to send: Send
Hi Ankur,
thanks for the replay and it was very clear for me to understand. But i have a similar kind of doubt from the following code

class MyClass
{
static int maxElement;
MyClass (int maxElement)
{
this.maxElement = maxElement;
}
}

public class Q19
{
public static void main(String[] args)
{
MyClass b1 = new MyClass(100);
MyClass b2 = new MyClass(100);
if (b1.equals(b2))
{
System.out.println("object have the same values");
}
else
{
System.out.println("object have different values");
}
}

}

why the out put is "object have different values";

thanks in advance.
+Pie Number of slices to send: Send
It's because you have two different objects and you have not overridden the equals method that is inherited from class Object.
+Pie Number of slices to send: Send
How can MyClass know i have to compare on "maxElement" so you have to override equals mathod in Myclass..


public boolean equals(Obj a){

if((obj instanceof Myclass)&&(this.maxElement == (Myclass)a.maxElement)
{
return true;
}else{
return false;
}

}



it is like to that...
[ September 12, 2006: Message edited by: Nilesh Patel ]
+Pie Number of slices to send: Send
In Stirng and Wrapper classes the equals() methods are overritten. So equals() methods return true if two different object value is same.
But for other classes we need to override the equals method.
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1103 times.
Similar Threads
Understanding Byte and == Op
Interesting Question
toString( ) method
Wrapper-Method-Q
* Byte == comparision
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:13:59.