Results 1 to 6 of 6
  1. #1
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Exclamation equals method to compare two objects?

    :confused:
    Is it possible to compare 2 objects like we do with Strings using equals() method.
    for e.g,
    class A
    {
    int a=10,b=10,c=10;
    }
    class Equals
    {
    public static void main(String s[])
    {
    A a,b;
    a=new A();
    b=new A();
    if(a.equals(b))
    System.out.println("We r same...");
    else
    System.out.println("We r not same...");

    }
    }

    this program gives me "We r not same....."
    How does equals() work in this program?
    if any one can help.....!!!

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    if you want to compare objects, use "==" operator

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    a == b
    returns true if and only if a is the same instance as b.

    a.equals(b)
    returns true if the instances are considered equal by the "equals" method,
    if you haven't written any "equals" method it will return a == b.


    I suggest you add a equals method to A:

    Java Code:
    @Override //Not needed
    public boolean equals(A obj)
    {
        return (obj != null) && (this.a == obj.a) && (this.b == obj.b) && (this.c == obj.c)
    }

  4. #4
    Yuvarajsinh is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Smile confusion

    thnx,
    That means equals method is overridden for all inbuilt classes of Java.
    and if we want the same functionality we have to override that method.
    Am i right?

  5. #5
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    It is good practice to override the equals method for your own classes and it's a must if there will be checks for equality of instances.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Yuvarajsinh View Post
    thnx,
    That means equals method is overridden for all inbuilt classes of Java.
    and if we want the same functionality we have to override that method.
    Am i right?
    Yep, you're right; as a rule of thumb, if you implement an equals( ... ) method you should also implement the hashCode() method so your class can be stored in a collection (think of a HashSet). Also note that if you want your objects to be 'storable' in such a collection you should be careful when you change the state (member variables) of your object that influence the result of your equals( ... ) method and/or your hashCode() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Mutable Objects having equals method
    By Yadu in forum Advanced Java
    Replies: 10
    Last Post: 08-22-2010, 04:43 PM
  2. equals method :::::HELP:::::
    By alihht in forum New To Java
    Replies: 5
    Last Post: 03-09-2010, 07:19 AM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  4. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 10:26 PM
  5. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 06:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •