12 Replies - 1049 Views - Last Post: 24 February 2013 - 12:23 PM Rate Topic: -----

#1 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 09:53 AM

I was studying my Java book and it states that the default behavior of equals() of class Object is to compare object references, which is true according to the API:

Quote

The equals method implements an equivalence relation on non-null object references

I understand that the relational operator "==" is primarily used to check the equivalence of primitive types, something like :
int x = 5;
int x2 = 5;
System.out.print(x == x2)


This would return true in this case.
However, if I set up something like this:
class Value
{	
	
}

public class TestForEquivalence
{
	public static void main(String [] args)
	{
		// Test #1 - Use equals() method to test object references
		Value v1 = new Value();
		Value v2 = new Value();
		System.out.println("v1.equals(v2): " + v1.equals(v2));
		
		// Test #2 - Use relational operator "==" to test object references
		System.out.print("v1 == v2: ");
		System.out.println(v1 == v2);


In test #1, I use the inherited equals() method of class Object to test my two object references v1 and v2. But, in test #2, I can also use the "==" operator to do the same thing.
So my question is, is there a difference between using equals() or "==" to test object references?

P.S - I am also aware that most of the Java standard classes will override equals() so that it will compare object contents. Example below:
// Test #3 - Using overridden equals() method in class Integer to test object CONTENTS
		Integer n1 = new Integer(50);
		Integer n2 = new Integer(50);
		System.out.println("n1.equals(n2): " + n1.equals(n2));


Result will be true in this case.
API : equals() method of class Integer.

Hope this makes sense. Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Difference between Relational operator "==" and equals()

#2 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 838
  • Posts: 2,572
  • Joined: 29-July 11

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 09:55 AM

It's the same for most classes, save for String.

Or is it? I always forget.

== is do they point to the same object on the heap.

This post has been edited by farrell2k: 23 February 2013 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 09:59 AM

The thing about the .equals method is that you can override it to give you own equality checks on two different objects. The == operator however cannot be changed.

Take a String for example

string1 == string2 // points to same place in memory check


This will test whether or not the two String objects are identical (they point to the same place in memory) and not whether or not they actually have the same value which you would thnk.

The equals operator by default will do the same thing on objects. The difference is you can provide your own implementation on your classes. String for example overrides it to test whether or not the actuals string value it represents is the same. Not the object reference.

string1.equals(string2) // has same string value


As primitives are on the stack and not the heap, pointers to places are not used. Therefore the == operator tests the value and not the pointer reference in primitives. The opposite holds for objects which go on the heap. == is used to test where the pointers point to, not the values.

Hope that makes sense.
Was This Post Helpful? 3
  • +
  • -

#4 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 838
  • Posts: 2,572
  • Joined: 29-July 11

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 10:05 AM

Here's one thing I do not like.

String one = "x";
String two = "x";
System.out.println(one == two);



true

String one = new String("x");
String two ="x";
System.out.println(one == two);



false.

It's confusing.
Was This Post Helpful? 2
  • +
  • -

#5 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 10:10 AM

Quote

It's the same for most classes, save for String.

Or is it? I always forget.

@farrell2k - They both work for String. :)/>
String s1 = "happy";
String s2 = "sad";
		
System.out.println("s1.equals(s2): " + s1.equals(s2));
System.out.println(s1 == s2);


Result is false in both cases.

Quote

String for example overrides it to test whether or not the actuals string value it represents is the same. Not the object reference. As primitives are on the stack and not the heap, pointers to places are not used. Therefore the == operator tests the value and not the pointer reference in primitives. The opposite holds for objects which go on the heap. == is used to test where the pointers point to, not the values.

@Ryano121 - I didn't know that. Thanks :)/>

This post has been edited by jjh08: 23 February 2013 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 10:12 AM

Yeah that's down to a compiler optimization using the String pool. It can see that the values are the same so it creates just one object and uses it multiple times in different places for efficiency. I believe we have a tutorial about String pools somewhere around here.

Edit - Tutorial: The String Pool

This post has been edited by Ryano121: 23 February 2013 - 10:13 AM

Was This Post Helpful? 3
  • +
  • -

#7 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 838
  • Posts: 2,572
  • Joined: 29-July 11

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 10:24 AM

I figured it had something to do with how the String is created.
Was This Post Helpful? 0
  • +
  • -

#8 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 10:27 AM

View Postfarrell2k, on 23 February 2013 - 10:05 AM, said:

Here's one thing I do not like.

String one = "x";
String two = "x";
System.out.println(one == two);



true

String one = new String("x");
String two ="x";
System.out.println(one == two);



false.

It's confusing.

That one is tricky. Both of them are objects and the contents are the same but the object references are not the same. Is that correct?

This post has been edited by jjh08: 23 February 2013 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 10:32 AM

Yeah sounds about right to me.

Both of the objects are exactly the same, with the same contents and the same location in memory. The only difference is the pointers you use to access the same object are different. Essentially you have two different pointers pointing to the same place. (it's a bit confusing to explain :P)

This post has been edited by Ryano121: 23 February 2013 - 10:33 AM

Was This Post Helpful? 1
  • +
  • -

#10 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Difference between Relational operator "==" and equals()

Posted 23 February 2013 - 10:41 AM

Quote

Essentially you have two different pointers pointing to the same place. (it's a bit confusing to explain :P)

Confusing indeed LOL :P
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Difference between Relational operator "==" and equals()

Posted 24 February 2013 - 10:58 AM

View Postfarrell2k, on 23 February 2013 - 12:05 PM, said:

Here's one thing I do not like.

String one = "x";
String two = "x";
System.out.println(one == two);



true

String one = new String("x");
String two ="x";
System.out.println(one == two);



false.

It's confusing.

Not confusing it is logic and easily explanable

View PostRyano121, on 23 February 2013 - 12:32 PM, said:

Yeah sounds about right to me.

Both of the objects are exactly the same, with the same contents and the same location in memory. The only difference is the pointers you use to access the same object are different. Essentially you have two different pointers pointing to the same place. (it's a bit confusing to explain :P/>)

Completly false :) Confusing to explain because you are confused :)

String x = "aaa";
String y = "aaa";


Computer optimization, as String are immuatable lets only define "xxx" once and have x and y point to it
So two different pointers pointing to the same place
String x = "aaa";
String y = new String("aaa");


new String() means force the creation of a new String not trying to optimize space allocation
So two different pointers pointing to two different places
Was This Post Helpful? 2
  • +
  • -

#12 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Difference between Relational operator "==" and equals()

Posted 24 February 2013 - 11:51 AM

That's what I said.
Was This Post Helpful? 1
  • +
  • -

#13 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 838
  • Posts: 2,572
  • Joined: 29-July 11

Re: Difference between Relational operator "==" and equals()

Posted 24 February 2013 - 12:23 PM

I think we should just let pbl go on and on. :)/>

do not like != do not understand.

Next someone will tell me that Java does not have multiple inheritance, when it does.

This post has been edited by farrell2k: 24 February 2013 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1