Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

==, .equals() and ints

 
Colm Shannon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was doing some study last night, and covered the differences between == and .equals()

So I know that == checks if two reference variables point to the same place, and that .equals does the exact same thing unless it's been overridden in the objects class (i.e. in the String class)

What I was left wondering was if I do this:

int i = 5;
int j = 5;

Why does == work. Have I not just created two ref variables pointing to two equal but DIFFERENT objects?

Thanks,
Colm
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are wrong if you say == checks the references. It checks the vaulue!
In case of objects the value is the reference to the object.
Primitives have no reference they just have value.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colm Shannon wrote:Why does == work. Have I not just created two ref variables pointing to two equal but DIFFERENT objects?

Because most of the Java wrapper classes cache frequently used values.

If you'd written:
int i = 5346;
int j = 5346;

'==' would NOT work; which is why you should AvoidTheEqualityOperator (←click).

For more information, you might find this page useful.

Winston
 
Colm Shannon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel Petermann wrote:You are wrong if you say == checks the references. It checks the vaulue!
In case of objects the value is the reference to the object.
Primitives have no reference they just have value.


Don't think I said anything wrong?

"The == operator compares two object references to see whether they refer to the same instance."
or
"If == is used to compare two objects then it compares the object reference and not the values."

Both are in line with my comment.

But thanks for the reply, your bit about primitives pretty much answered my question.
 
Colm Shannon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Colm Shannon wrote:Why does == work. Have I not just created two ref variables pointing to two equal but DIFFERENT objects?

Because most of the Java wrapper classes cache frequently used values.

If you'd written:
int i = 5346;
int j = 5346;

'==' would NOT work; which is why you should AvoidTheEqualityOperator (←click).

For more information, you might find this page useful.

Winston


Just checked and it did work. Thanks or the link though :-D
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colm Shannon wrote:Just checked and it did work. Thanks or the link though :-D

Beg pard. I meant to say:
Integer i = 5346;
Integer j = 5346;

Doh-h-h!

And the fact is that your original code was NOT creating "two ref variables pointing to two equal but DIFFERENT objects"; it was creating two primitives.

Winston
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colm Shannon wrote:
Don't think I said anything wrong?

"The == operator compares two object references to see whether they refer to the same instance."
or
"If == is used to compare two objects then it compares the object reference and not the values."

Both are in line with my comment.

But thanks for the reply, your bit about primitives pretty much answered my question.


Just that int is not an object is all.
 
Colm Shannon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Colm Shannon wrote:Just checked and it did work. Thanks or the link though :-D

Beg pard. I meant to say:
Integer i = 5346;
Integer j = 5346;

Doh-h-h!

And the fact is that your original code was NOT creating "two ref variables pointing to two equal but DIFFERENT objects"; it was creating two primitives.

Winston


Ah got yeah. Perfect thanks. Yeah I just wasn't clear on the difference between Primitives and Objects. Thanks guys!
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it is a bit more complicated; there are a few floating‑point primitives where the same value can return false from == and different values can return true from ==. Look at my post here. Find out about nan values. Also try replacing nan in my code with +0.0f and -0.0f.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic