Sobes.tech
Junior — Senior

Comparison of Integer objects and the role of value cache

livecode

Task condition

The behavior of comparing Integer objects using the == operator is examined. It explains why references may point to the same object for numbers in the range from -128 to 127, while for larger values, each object is created separately.

public class Test2 {
    public static void main(String[] args) {
        Integer first = 100;
        Integer second = 100;

        Integer third = 200;
        Integer fourth = 200;

        System.out.println("first == second: " + (first == second));
        System.out.println("third == fourth: " + (third == fourth));
    }
}