• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Doubt on "=="

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic