• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

difference between == and equals( )

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain the difference between == and equals() with a good example.
This is a bit confusing for me.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== is means address comarision operator. s1==s2 means if both points to same location it will return true otherwise return false.In this case both s1 and s2 must be contain some relationship otherwise it will give compiletime error.

equals method is available in Object class. In object class it is defined based on == operator.
Here the return type is boolean.
s1.equals(s2) if s1 and s2 are different then it will return false , s1 and s2 are points to duifferent location then it will return false. if both points to same location it will return true. In this case we will never get compile time error.

If you want to change the behaviour of equals method you can change by overriding.
This was happened in String and Wrapper classes. In this 2 cases they override like content comparision instead of address comparision.
 
radhika ayirala
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class testEqual
{
public static void main(String[] args)
{
testEqual te = new testEqual();
testEqual abc = new testEqual();
int i = 5;
int j = 5;
System.out.println("te == abc------->"+(te == abc));
System.out.println("i == j------->"+(i == j));
}
}

please explain this example
 
raghu nagabandi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println("te == abc------->"+(te == abc));
System.out.println("i == j------->"+(i == j));
 
raghu nagabandi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println("te == abc------->"+(te == abc));
In above case:
Both te and abc are same type so you will not get any comipile time error.
and both are points 2 different location so it will return false.

System.out.println("i == j------->"+(i == j));
In case of numeric comarision don't think as a address comparision.just it will compare there values whether same or not. if it is same return true otherwise false.In this case if you compare int and double value , then int is automatically promoted to double perform comparision. It means automatic type conversion is possible.

== means comparision. For normal variables or constants we can compare directly , but where as objects we can not. so in case of objects we will say == as a address comparision operator.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are always working with a reference variable when you do the comparison. If the reference variable represents an object, the reference variable value is a memory location . If the reference variable is a primitive, then the variable value is the value of the primitive. So for 2 objects, if the reference variable value is the same address (they point to the same object that is at that location) then == is true.

Equals does not exist for primitives. For Objects, you can override the equals method that gets inherited from Object. You implement what equals means for an object. Usually you compare the internal state of the objects and if the internal state is equal then the objects are equal.
[ August 28, 2007: Message edited by: Michael Ku ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic