Sobes.tech
Junior — Senior

Differences between comparing primitive int and Integer object

livecode

Task condition

Code overview of a method illustrating how value comparisons and reference comparisons differ for the primitive type int and its object wrapper Integer, as well as demonstrating the Integer object caching mechanism.

private static void intPool() {
    int x = 127;
    int y = 127;
    System.out.println(x == y);

    x = 300;
    y = 300;
    System.out.println(x == y);

    Integer obj1 = 127;
    Integer obj2 = 127;
    System.out.println(obj1 == obj2);

    obj1 = 300;
    obj2 = 300;
    System.out.println(obj1 == obj2);
}