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.
Integer....intValue()of the Integer-objects, and also gives additional information about it in general. But it fails to answer the actual question asked by the OP. I've therefore voted to re-open.Integerthe.valueOfmethod does the following:return new Integer(parseInt(s, 10, false));. As you can see, it uses.parseIntinternally, and putting it in anInteger-constructor to create anInteger-instance..parseIntdoes indeed return a primitive:return parseInt(s, 10, false);. However, since you save it in anIntegerfield, autoboxing occurs, which implicitly behaves somewhat something similar to the use of the constructor in the.valueOfmethod.