Is it ok to compare immutable objects in Java using == instead of equals
问题 Consider two references of type Integer that call the static factory method valueOf as shown below:- Integer a = Integer.valueOf("10"); Integer b = Integer.valueOf("10"); Considering that Integer is immutable, is it ok to compare a and b using == instead of using equals method. I am guessing that the valueOf method makes sure that only one instance of Integer with the value 10 is created and a reference to this instance is returned for every Integer created with a value 10. In general, is it