Results 1 to 14 of 14

Thread: == and equals()

  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default == and equals()

    Hello
    I have some questions about class,object and variable.

    when i write
    Java Code:
    int x=10;
    means that there is a place in memory with 4 bytes that this place has
    10 value and this place has an address.ok?
    1)what is the meaning of int?int is a class or an object?


    2)when i write
    Java Code:
    int x=10,y=10;
    if(x[b]==[/b]y){}
    means that two places of memory with different address which have 10 value
    are equal?


    3)when i write
    Java Code:
    public class B
    {
    	public static void main(String args[])
    	{
    	  Integer n1 = new Integer(47);
    	  Integer n2 = new Integer(47);
    	  System.out.println(n1==n2);//Result is false
    	  System.out.println(n1!=n2);//Result is true
    	}
    }
    means that the address of two objects with different address which have 10 valueare equal?


    4)when i write
    Java Code:
    public class B
    {
    	public static void main(String args[])
    	{
    	  Integer n1 = new Integer(47);
    	  Integer n2 = new Integer(47);
    	  System.out.println(n1.equals(n2));//Result is true
    	}
    }
    means equals(java.lang.Integer) differs with ==?what difference?


    5)when i write
    Java Code:
    class Value
    {
    	int i;
    }
    public class B
    {
    	public static void main(String args[])
    	{
    	  Value n1 = new Value();
    	  Value n2 = new Value();
    	  n1.i=n2.i=100;
    	  System.out.println(n1.equals(n2));//Result is false
    	}
    }
    why does result in this code differ with result in previous code? both of them
    use from equals().

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You need to understand Primitive types vs Reference types..
    1.)int is one of the primitive types. There are eight(?) of them.

    2.) Variables of primitive types hold the actual value of the variable while variables of reference types hold addresses of where the objects are in memory.
    3,4.)When you compare with == you are comparing equality of the references.
    For primitive types the comparison returns true if the values are the same because the references hold the actual values so if the values are equal the references will be equal.

    5.)For reference types, when two references point to the same object, it doesn't mean that the references themselves are equal. The equals method (when correctly overriden) looks at the actual objects pointed to by the references so that the equals test can return true if the objects pointed to are equal. In your last example, the Integer class has the equals method correctly overriden so equals returns true while your Value class has no appropriate equals method.

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

    Default

    You used the Integer class in your examples; that class plays a few tricks; e.g. if you do this:

    Java Code:
    Integer i1= 42;
    Integer i2= 42;
    System.println(i1 == i2);
    ... it will print 'true'. The primitive value 42 was 'autoboxed' and the Integer class keeps a cache for a few numbers (the range [-128, 127] to be exact). The above example makes both i1 and i2 refer to the same Integer object that contains the primitive value 42 so i1 == i2 is true. This is because of the caching; if you would have done this instead:

    Java Code:
    Integer i1= 142;
    Integer i2= 142;
    System.println(i1 == i2);
    It woud've printed false because the primitive value 142 will not be cached and two different objects are created.

    The == operator compares pointers (references) and primitive values, i.e. it doesn't compare the content of objects (non-primitive values) because it doesn't know how to do that. You have to use the equals( ... ) method for that which is always defined because it is defined as a public method in the Object class. The default definition equals the == operator semantics though so it needs to be overridden to make a bit of sense (e.g. the String class overrides this method).

    kind regards,

    Jos

  4. #4
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I defined an equals method for my program.

    Java Code:
    abstract class EqualClass
    {
    	abstract boolean equals(Value temp);
    }
    class Value extends EqualClass
    {
    	int i;
    	boolean equals(Value t)
    	{
    		if(this.i==t.i)
    		return true;
    		else
    		return false;
    	}
    }
    public class B
    {
    	public static void main(String args[])
    	{
    	  Value n1 = new Value();
    	  Value n2 = new Value();
    	  n1.i=n2.i=100;
    	  System.out.println(n1.equals(n2));
    	}
    }
    now, how can i define my equals method in java library?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    I defined an equals method for my program.

    Java Code:
    abstract class EqualClass
    {
    	abstract boolean equals(Value temp);
    }
    class Value extends EqualClass
    {
    	int i;
    	boolean equals(Value t)
    	{
    		if(this.i==t.i)
    		return true;
    		else
    		return false;
    	}
    }
    public class B
    {
    	public static void main(String args[])
    	{
    	  Value n1 = new Value();
    	  Value n2 = new Value();
    	  n1.i=n2.i=100;
    	  System.out.println(n1.equals(n2));
    	}
    }
    now, how can i define my equals method in java library?
    Throw out this code, it's a very very bad idea.

    Simply override Object's equals method in any class that needs to do this, that's it. For e.g.,
    Java Code:
    class Value 
    {
       int i;
       
       @Override
       public boolean equals(Object obj) {
          if (obj == null) {
             return false;
          }
          if (!(obj instanceof Value)) {
             return false;
          }
          else {
             Value otherValue = (Value) obj;
             return i == otherValue.i;
          }
       }
    }
    Last edited by Fubarable; 01-04-2010 at 04:04 PM.

  6. #6
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    Please Describe bold Terms more.I could not undrestand well.

    Java Code:
    class Value
    {
    	int i;
    
    	   @Override
       public boolean equals(Object [b]obj[/b]) {
          if (obj == null) {
             return false;
          }
          if (!(obj instanceof Value)) {
             return false;
          }
          else {
             Value otherValue = [b](Value) obj[/b];
             return i == otherValue.i;
          }
       }
    }
    public class B
    {
    	public static void main(String args[])
    	{
    	  Value n1 = new Value();
    	  Value n2 = new Value();
    	  n1.i=50;n2.i=100;
    	  System.out.println(n1.equals([b]n2[/b]));
    	}
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    Please Describe bold Terms more.I could not undrestand well.
    When you override a class's method, you have to use the exact same method signature that the parent class uses, else the method will not truly be overridden.

    If you look at the Object class in the Object API, you'll see that it's method looks like so:

    Java Code:
    public boolean equals(Object obj)

    and so your equals method signature needs to look exactly the same. An exception is that "obj" is simply a parameter and can have any name you choose, but it must be of type Object and no other. In other words, this is OK:

    Java Code:
    public boolean equals(Object foo)

    but this signature won't fly:
    Java Code:
    public boolean equals(Value foo)
    since it changes the type of the parameter. When you call this method, you have no guarantee that the parameter passed is a Value object nor that it isn't null, so you have to test for this first. After you've done your testing and you know that the parameter isn't null and is a Value object, then cast it to a Value type and use it as such.

  8. #8
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Ok.Thank you very much.
    Throw out this code, it's a very very bad idea.
    if i used from that my code, what was its result? it will be very bad?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by arefeh View Post
    Ok.Thank you very much.

    if i used from that my code, what was its result? it will be very bad?
    You're reinventing the wheel and in the process using a non-standard and poorly implemented equals method. If later someone else tries to use your code, extends the class, and then uses the super.equals method in their equals override, they'd be in for a rude shock.

  10. #10
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I am sorry if my question is low level. because I am beginner in java, I can learn things better with asking question.
    -in first post, I asked about primitive data types. In java, they are not object. They are simple that do not have behavio(u)r.

    From: Bruce[1].Eckel.Thinking.In.Java.4th.Edition.Dec.2007.eBook-BBL
    One group of types,which you will use quite often in your programming, gets special treatment. You can think of these as primitive types. The reason for the special treatment is that to creat an object with new-especialy a small,simple variable-is not very efficient, because new places objects on the heap. For these types Java falls back on the approach taken by C and C++. That is, instead of creating the variable by using new, an “automatic” variable is created that is not a reference. The variable holds the value directly, and it is placed on the stack, so it is much more efficient.
    Java determines the size of each primitive type. These sizes do not change from one machine architecture to another as they do in most languages. This size invariance is one reason Java programs are more portable than programs in most other languages.
    Now, where are these primitive data types when JDK instuled? Are these just for compiler is visible? I did not find file or folder about them.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What? Are you looking for a folder containing ints and longs and chars on your harddrive somewhere?

  12. #12
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    I am sorry if my question is low level. because I am beginner in java, I can learn things better with asking question.
    -in first post, I asked about primitive data types. In java, they are not object. They are simple that do not have behavio(u)r.



    Now, where are these primitive data types when JDK instuled? Are these just for compiler is visible? I did not find file or folder about them.
    so you looking for the location of the primitive data types folder.they will be defiend inside JDK ofcourse. Those data types are not for compiler visibility.

    i'll give u reason why they're not

    whenever you compile your code the compiler puts those in a seperate stack. which has unique addresses on it. they can be accessed in runtime. I mean
    int x =2;

    System.out.println(x);

    1st line puts inside the stack.
    2nd when executed. it prints the value of x.

    so they're not meant for compiler visibilty. they just give aid coders for a meaningful output too.

    if you do int x and suddenly you get a string.that would suck wouldnt it

  13. #13
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello

    What? Are you looking for a folder containing ints and longs and chars on your harddrive somewhere?
    For me the behavio(u)r of compiler is attractive that a compiler how knows this data type is primitive or non-primitive, or these are where and ….
    In my idea, if anyone knows behavio(u)r of compiler, understanding of programs becomes easier.
    However, I thank you that pay attention and answer my questions.

  14. #14
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

Similar Threads

  1. equals error
    By sysout in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-06-2009, 03:23 AM
  2. == is same as .equals()??
    By DrMath in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 04:57 AM
  3. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 10:26 PM
  4. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 06:52 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 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
  •