Search code examples
javaintegerheap-memoryimmutabilitywrapper

Why are two references of Integer equal to a certain point?


Why the Integer objects do not behave the way String objects behave?

I read that the reason was performance but can not understand how it would perform better?

Look at the code below for example :

public class MyClass{
    public static void main(String[] args){
        String one = "myString";
        String two = "myString";
        System.out.println(one == two); // true

        String oneLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
        String twoLong = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in leo at massa vehicula rhoncus quis eu mauris. Pellentesque non nulla convallis, tempus augue sed, sollicitudin risus. Aenean posuere nulla ipsum, at faucibus massa dignissim non. Duis felis felis, iaculis eu posuere id, elementum id nulla. Fusce tristique arcu vitae consectetur vehicula. Mauris tincidunt nunc placerat tortor rhoncus, eget venenatis felis dapibus. Sed scelerisque ligula congue ligula elementum hendrerit. Proin et mauris vestibulum, rutrum ante ut, sollicitudin massa. Fusce tempus mattis eleifend. Phasellus ut ante turpis. Suspendisse eu leo nec elit ornare rhoncus sed nec ex. In at tellus mi.";
        System.out.println(oneLong == twoLong); // true

        Integer first = 1;
        Integer second = 1;
        System.out.println(first == second); // true

        Integer third = 500;
        Integer fourth = 500;
        System.out.println(third == fourth); // false
    }
}

Here are the questions I found but no response was given there :

Why Integer In Java is Immutable

Is Integer Immutable?


Solution

  • Generally is a good idea to mantain an object as immutable as possible because an immutable object can be shared without problems in a multithreading enviroment.

    Creating n copies of the same Integer is possible, ma that copies are immutable. Note that also for String is possible to create n different copies of the same String. To do that you need to explicitly use the new keyword.

    Two objects are the same if compared with the operator == returns true. Two objects have the same content if compared with the method equals returns true.

    obj1 == obj2       // if true obj1 and obj2 are the same
    obj1.equals(obj2)  // if true obj1 and obj2 have the same content 
                       // (can be the same or not)
    

    In your example you have two Integer with the same content (so equals returns true) but different memory locations (so == returns false)