To my understanding == compares that they are the same object while .equals() compares that the content is the same. So I created the code below to make sure I understand the difference between == and .equals() To my understanding in this code only the if statement with .equals should print out but they both print. Not sure what is going on here any help would be appreciated.
Java has constant pools that cache certain objects. For instance, String literals go into the String pool, so if you have "abc" twice in your code, there's only one object, and == will return true.
There are also cached instances for small values of the integral type wrappers. I think the default is -128..127, but it's configurable by a JVM startup option. Any time you use autoboxing, or call the appropriate valueOf() method (which is what autoboxing does), it will retrieve the cached instance if it's present, else create a new one.
So when you do Integer one = 1; it just gets a reference to the cached object, rather than creating a new Integer object. So every case of Integer something = 1; will be pointing to the same object.