• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

==, .equals() and ints

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic