Integer x1 = Integer.parseInt("4");
Integer y1 = Integer.parseInt("4");
Integer x2 = Integer.parseInt("444");
Integer y2 = Integer.parseInt("444");
System.out.println(x1==y1); // true
System.out.println(x2==y2); // false ???
Integer a1 = Integer.valueOf("4");
Integer b1 = Integer.valueOf("4");
Integer a2 = Integer.valueOf("444");
Integer b2 = Integer.valueOf("444");
System.out.println(a1==b1); // true
System.out.println(a2==b2); // false
I understand why the third and fourth output print true and false. This is because valueOf
returns an object, and wrapper classes cache objects that have values in the range of -128 to 127. If valueOf
is passed any value in that range, it should reuse the object in the cache. Otherwise, it will create a new object.
Now, why does the second output print out false? I thought parseInt
returns a primitive, not an object like valueOf
does.