• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

whats the difference between == and equals

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can some explain me the difference between == and equals?
why equals is used with object ans string only?
savita
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi savita !
There is a lots of difference in both. == does only "coparision" that is as visible to our eye as well say a string or numbers etc. But with equals() the comparision is deeper
For example 1-1-2001 and 1-jan-2001 are equal from the 'moment in time" consideration so == will return false whilst equals() will return true if used with apropriate calsses.
I hope that explains it all.
parag kulkarni
mumbai
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals is a method defined in the Object class. It operates on objects and takes in an argument of Object and returns a boolean value. It has been declared as public boolean equals(Object o). This method has been overriden in various classes like String, Boolean, Integer etc.
In String class equals compares the characters inside String object, whereas == compares object references.
The same holds good for Integer class. equals method - where the actual int value is compared whereas == compares object references.
Check the following programme out. It will make things more clear.
class EqualsTest{
public static void main(String arg[]){
String a = new String("Test");
String a1 = new String("Test");
Integer i = new Integer(100);
Integer i1 = new Integer(100);
System.out.println("a.equals(a1) " + a.equals(a1));
System.out.println("i.equals(i1) " + i.equals(i1));
System.out.println(a == a1);
System.out.println(i == i1);

}
}
Hope this helps.
Rekha
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about this code,is the test for == correct.
class EqualsTest{
public static void main(String arg[]){
String a = "Test";
String a1 = "Test";
Integer i = 100;
Integer i1 = 100;
System.out.println("a.equals(a1) " + a.equals(a1));
System.out.println("i.equals(i1) " + i.equals(i1));
System.out.println(a == a1);
System.out.println(i == i1);

}
}
what is the output.
thanks
 
Rekha Sivadasan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get a compile time error for the programme since you can't create an Integer object like that.
As far as String are concerned both the output will be true since Strings are immutable and when similar strings are created without the new keyword both a and a1 refer to the same object.
 
savita jadhav
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everybody for solving my problem.got the concept
savita
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Savita,
You may find the self review questions on my homepages useful, if you need to test your == and equals() concepts. http://www.tipsmart.com/javacert/selfreview/objequiv.htm
-Sandeep Nachane
 
reply
    Bookmark Topic Watch Topic
  • New Topic