# Integer VS int

Ranch Hand
Posts: 58
Hi Friends,

I have a problem in the following code.

When I run this code, it prints "true false".

Why it is printing "false" when comparing 2200 & 2200.

Thanks & Regards

Suresh.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
An int is automatically converted to an Integer by creating an Integer object. Each time this conversion is done, a new object must be created. Remembering that the "==" operator returns true only if both operands refer to the same physical object, you will hopefully now understand. The two ints are converted to Integers by constructing two separate Integer objects, which the "==" operator correctly reports as being unequal.

Never use "==" to compare Integer objects (or Float, Double, Short, Character, Byte, Long, BigDecimal...) for equality. Compare numbers in their primitive form -- i.e., work with int, short, long, double.

Brian Lang
Ranch Hand
Posts: 43
You're comparing two Integer object references, not two int (primitive type) values. You want to use compareTo() when attempting to find out whether or not two Integer values are the same.

Patricia Samuel
Ranch Hand
Posts: 300
If we use == operator on Object, it means we are comparing references. In the first case you are comparing references that are referring to same object. Hence it is showing True in first case. In second case you are comparing the references that are not referring to same object. Hence, printing False in this case.

Thanks

Ranch Hand
Posts: 58
I am sorry to say that I have not got the answer yet. What I am asking is, when comparing two Integer objects of value 12 (twelve) its returning TRUE. And when comparing two Integer objects of value 2200 its returning FALSE. Could you please throw some light on it.

Thanks.

Suresh.

Brian Lang
Ranch Hand
Posts: 43
Suresh> In my opinion, your question was answered neatly by Patricia. You have four int values, correct? You pass those in to your 'method' method, and they are converted to Integer references. Apparently the JVM decided to take the two Integer objects representing the value 2200 and stick them into different places in memory, so they are not pointing to the same reference (not equal). Then it decided to use the same register for the value 12, and you get a boolean TRUE returned based on the fact that they do point to the same reference.

I just realized I explained that in the reverse order, but it's the same point.
[ November 13, 2008: Message edited by: Brian Lang ]

Gamini Sirisena
Ranch Hand
Posts: 378
This is the defined behaviour for Java when Autoboxing. Check the relevant Java Language Specification section. Read the discussion section there also.

For ints, those between -128 and 127 (inclusive) autboxed Integer objects will return true for the check ==. It is said that these integers are cached and in a constant pool.
But if you just do new Integer(int) the objects created will always be different.

You can write a small program and check the above limits.

Ranch Hand
Posts: 58
Gamini,

Thank you so much for the answer. I think I got it, this is where I got confused. I really appreciate it.

Thanks again,

Suresh.

Campbell Ritchie
Sheriff
Posts: 50685
83
Originally posted by Brian Lang:
You want to use compareTo() when attempting to find out whether or not two Integer values are the same.

You mean equals(), surely?

Brian Lang
Ranch Hand
Posts: 43
Originally posted by Campbell Ritchie:

You mean equals(), surely?

No, I meant compareTo(). As in to compare the two values, not the two references, which is what the OP was attempting to do. Is that not correct?

Brian Lang
Ranch Hand
Posts: 43
Originally posted by Gamini Sirisena:
This is the defined behaviour for Java when Autoboxing.

Good info, thanks.

Campbell Ritchie
Sheriff
Posts: 50685
83
Originally posted by Brian Lang:

No, I meant compareTo(). As in to compare the two values, not the two references, which is what the OP was attempting to do. Is that not correct?

You use compareTo() which is implementing the Comparable<T> interface, to find which way two values differ. There are strict instructions in the Comparable<T> API documentation.
I thought the use of == meant the original poster was trying to find whether the values represented by those two Integers were the same, in which case you usually use the equals() method. This is overridden from the Object class and there are instructions, also strict, in the Object class documentation.

There are a few exceptions:
• Some classes represent object whose state changes frequently, eg StringBuilder. They have an un-overridden equals method. To compare equality of the values try builder1.toString()equals(builder2.toString());
• Classes which are implicity different from each other, eg Thread; every Thread is regarded implicitly as different from every other Thread.
• A few classes like BigDecimal have more information than their value; if you look in the BigDecimal documentation it says 2.0 is not equal to 2.00. Then bd1.compareTo(bd2) == 0 may be necessary.
• And we all know the Object#equals method uses the == test. Of course auto-boxing produces some slightly different results.