The Artima Developer Community
Sponsored Link

Java Answers Forum
equals doubt

3 replies on 1 page. Most recent reply: Sep 14, 2005 5:18 AM by Jeff Thomson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Paulo Rezende

Posts: 2
Nickname: rhiegen
Registered: Apr, 2002

equals doubt Posted: Apr 26, 2002 12:44 PM
Reply to this message Reply
Advertisement
// ex1 suppose I have

class Value {
int i;
}

public class EqualsMethod2{
public static void main( String[] args){
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100; // ***
System.out.println(v1.equals(v2));
}
}

The answer is false.
Why is it false? I didn?t understand it.
In my opinion, since equals tests the contents of the object datamember i, I understood that in line // ***
v2.i receives 100, v1.i receives the memory address of v1.i, and so it?s false. Is my conclusion correct?
My doubt is about the difference between == and equals. I'm reading Thinking in Java and as I got to this part this doubt appear to me. Any suggestions?

Thanks in advance,

Rhiegen


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: equals doubt Posted: Apr 26, 2002 2:11 PM
Reply to this message Reply
Sorry.Your conclusion is not correct. Let me explain you about equals() method in brief.
When you write v1.equals ( v2 ) , then equals compares the references of v1 and v2. Since v1 and v2 are two different objects equals() returns false. By default, equals for any objects doesn't do any comparision on their data members. In your case v1.equals ( v2 ) doesn't do anything on v1.i and v2.i . It just compares the references (pointer) of v1 and v2. However, you can override equals () method for your class and then you can compare any data members for your class inside your equals() method. For example, String class overrides equals() method and compares the content stored in the String object. For your class Value you can override equals method as:
 public class Value {
   int i ;
   public boolean equals ( Object aValue ) {
      if ( this == aValue ) {
        return true;
       }
      if ( aValue instanceOf Value ){
          Value v = (Value) aValue;
          if ( i == v.i ) {
             return true;
          }
      }
      return false ;
 
   }
 }


Now , if you say v1.equals ( v2 ) then it will return true in your case , because in equals() method in Value class compares i data member value.

The == operator always compares the reference of two objects. In case of primitive data type it compares the values.
Therefore, equals() can compare (and by default it does compare references) content as well as references of two objects. To compare the contents of two objects you must override equals method in that class.

Thanks
Kishori

susanth

Posts: 1
Nickname: sush3152
Registered: Sep, 2005

Re: equals doubt Posted: Sep 9, 2005 7:54 AM
Reply to this message Reply
i have some doubts in java and i want to pst it in this site .pls help me how is this possible . get me link of the qustioning page.

Jeff Thomson

Posts: 14
Nickname: thomson
Registered: Sep, 2005

Re: equals doubt Posted: Sep 14, 2005 5:18 AM
Reply to this message Reply
To post a question in the forum you need to login. Use this link after login http://www.artima.com/forums/forum.jsp?forum=1
On the left side of the page there you'll find "Java Answers Forum" title under which you have "post new topic" link between "back to forum list" and "search forum" links. Click this link to get started.

----------------
Jeff Thomson
http://www.devsquare.com
Online Application Development

Flat View: This topic has 3 replies on 1 page
Topic: public class Previous Topic   Next Topic Topic: Javac

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use