Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on "=="

 
R .sourav nayak
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R .sourav nayak
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you have two different objects and you have not overridden the equals method that is inherited from class Object.
 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sureshkumar Chinnappan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic