Sobes.tech
Junior — Senior

Operator == behavior when comparing Integer objects

livecode

Task condition

Investigate how the comparison of two Integer objects using the == operator works with different values. Explain the results obtained, considering the internal cache of Integer objects in the range from -128 to 127.

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

        Integer third = 300;
        Integer fourth = 300;

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