File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on "==" " Watch "Doubt on "==" " New topic
Author

Doubt on "=="

R .sourav nayak
Ranch Hand

Joined: May 14, 2006
Posts: 67
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

Joined: Dec 27, 2005
Posts: 1249

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.


The Best way to predict your future is to create it - Every great individual common man
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

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

Joined: May 14, 2006
Posts: 67
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

Joined: Aug 03, 2002
Posts: 7729
It's because you have two different objects and you have not overridden the equals method that is inherited from class Object.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91
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 ]

Nilesh Patel
SCJP 1.5 - 87%
Sureshkumar Chinnappan
Greenhorn

Joined: Feb 15, 2006
Posts: 27
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.


Warm Regards,<br />Suresh Kumar<br /> <br />SCJP 5.0<br />SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on "=="