-1

Kindly Somebody explain me, Why the answer is true & false

public class IntegerTest {

        public static void main(String[] args) {

            Integer x = 1000;
            long y = 1000;
            Integer z = 1000;
            System.out.println(x == y);
            System.out.println(x == z);

        }
    }
alex
  • 10,900
  • 15
  • 70
  • 100
Gopal Singh
  • 44
  • 2
  • 9
  • As of my knowledge, Integer is a class, thats way it will also check the reference. But in case of long it will compare with the primitive data type ie. int. So is it correct ? – Gopal Singh Apr 03 '14 at 11:09
  • 2
    Please look at the link http://stackoverflow.com/questions/1514910/when-comparing-two-integers-in-java-does-auto-unboxing-occur – vishal Apr 03 '14 at 11:13
  • Interestingly, if you had used smaller values for `x` and `z`, say 10, then the second check would have returned `true`. – JonK Apr 03 '14 at 11:24
  • Yes, It is correct. Why ? – Gopal Singh Apr 03 '14 at 11:26
  • If you look at the `Integer` class source code, you'll notice that it contains an `IntegerCache` static inner class which contains an array of `Integer` objects representing -128 up to 127. When you auto-box, this cache is checked to see if it contains that value, if it does, you get a reference to the cached `Integer` back. – JonK Apr 03 '14 at 11:32

7 Answers7

8

x == y is a value comparison (because y is a primitive type), so 1000==1000 -> true.

x == z is an object reference comparison - object x is not an object z. They hold the same values, but they are still two completely different objects. So if you compare them you get false.

Boxing & Unboxing conversions in JLS section 5.1.7.

alex
  • 10,900
  • 15
  • 70
  • 100
2

This has to do with the fact that Java has primitive types (like int) and reference types (like Integer).

When a reference type is compared to a primitive type, the reference types actual value is compared to the value of the primitive type. However when two reference types are compared, the references themselves are compared, not the values.

geoand
  • 60,071
  • 24
  • 172
  • 190
0
System.out.println(x == y);//this will compare referance
System.out.println(x == z);//this will compare value

referances are different and values are same so it gives such op

you may would like to read Auto boxing and Unboxing

dev2d
  • 4,245
  • 3
  • 31
  • 54
0

The first is true cause its exactly the same value. that's clear right? the second is false cause an long is a floating point value which is 1000.0. so if you work with comparing floting point values with integers you need to add a type cast or use compareTo()

Hope that helps

malle
  • 334
  • 4
  • 17
0

Checks if the values of two operands are equal or not, if yes then condition becomes true. Equality Test Operators ==, !=

The == operator tests if two values are the same, so (x == 6) is true if x contains the value 6. The not-equal operator, !=, is the opposite, evaluating to true if the values are different. Typically, you use == and != with primitives such as int and boolean, not with objects like String and Color. With objects, it is most common to use the equals() method to test if two objects represent the same value.

The similarity of == and .equals() can be confusing, so here is a suggested rule: Every value in Java is either a primitive (e.g. int) or an object (e.g. String). Use ==, <, >=, etc.. with primitives like int. Use .equals() with objects like String and Color. Since the dereference dot (.) only works with objects, you can remember it this way: if it can take a dot then use .equals() (e.g. a String), otherwise use == (e.g. an int).

It is also possible to use == with objects. In that case, what == does is test if two pointers point to exactly the same object. Such as use of == is a little rare in Java code, and so it's simpler to concentrate on using equals() with objects, and == only with primitives.

Jag
  • 123
  • 3
  • 14
0

When you ask Java to compare x == y you ask it to compare Integer (class) with 1000 (long , which is a primitive type). In this case Java will auto unbox x and try to compare the values.

However when you try to compare two classes (even two Integers), Java will compare the addresses, which will be false.

Oron
  • 931
  • 7
  • 9
  • Thanks Oron, I was expecting the same. – Gopal Singh Apr 03 '14 at 11:21
  • What you might not expect though, as @JonK wrote is that if you would have used 10 instead of 1000 even the 2nd case may have printed true. Either use `.equals` or cast before comparing `(int)x == int(z)` etc. – Oron Apr 03 '14 at 11:27
  • Hello Oran, See Jonk Comment. For smaller values like 10 it will return true. plz tell me, Why ? – Gopal Singh Apr 03 '14 at 11:28
  • Yaa, i got the point. But my question is, In case of smaller values, Will it not compare the addresses ? – Gopal Singh Apr 03 '14 at 11:33
  • It will still compare the address. The thing is that when you use a smaller number the address might be the same. – Oron Apr 03 '14 at 11:39
  • I will apologize to ask again, But what do u mean, for smaller values the address may be the same. It is not a proper reason. I know the difference bw equalTo & == . equalTo only compares the vsalues whereas == compares values as well as the references(addresses). For smaller values how the pointer may same ? Doubtful – Gopal Singh Apr 03 '14 at 11:46
  • It is just a behavior in Java. If you would have used '10' instead of '1000' in your example both prints would have been 'true'. JonK explain it above, and vishal linked to a different thread with all the relevant info. – Oron Apr 06 '14 at 07:10
0

Integer is a wrapper type, an Object type associated with the primitive type int, so, although it is an object type, it can behave as a primitive type. When comparing with a primitive type using ==, the Integer object behaves as a primitive type and the values of both variables x and y are compared returning true. When comparing with another object, using == we are asking is both objects are identical, which is not the same as being equal. Integer objects are equals if their value if the same, and this can be checked using .equals() . So x.equals(z) would be true. But x and z are not identical, if we change the value of z, the value of x keeps the same, they are different objects so x==z is false.

rixhack
  • 1
  • 1