| Author |
==, .equals() and ints
|
Colm Shannon
Greenhorn
Joined: Feb 03, 2013
Posts: 4
|
|
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
Joined: Jul 19, 2011
Posts: 175
|
|
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.
|
Please correct my English.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4749
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Colm Shannon
Greenhorn
Joined: Feb 03, 2013
Posts: 4
|
|
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
Joined: Feb 03, 2013
Posts: 4
|
|
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
Joined: Mar 17, 2011
Posts: 4749
|
|
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
Joined: Jul 19, 2011
Posts: 175
|
|
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
Joined: Feb 03, 2013
Posts: 4
|
|
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
Joined: Oct 13, 2005
Posts: 32675
|
|
|
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.
|
 |
 |
|
|
subject: ==, .equals() and ints
|
|
|