Answers

Question and Answer:

  Home  Java Software Engineer

⟩ Tell me why is the code printing true in the second and false in the first case?

JVM’s cache behavior can be confusing, so this question tests that concept. The second output is true as we are comparing the references, because the JVM tries to save memory when the Integer falls within a range (from -128 to 127). At point 2, no new reference of type Integer is created for ‘d’. Instead of creating a new object for the Integer type reference variable ‘d’, it is only assigned with a previously created object referenced by ‘c’. All of these are done by JVM.

 123 views

More Questions for you: