Are you sure your code is compilable?
Float l1=30L;
This statement cannot be compiled. I think you wanted to say something like
. There are two aspects to this question. When the autoboxing happens, the original code gets replaced to
after compilation. Now if you see valueOf() implementation of non floating point numbers (i.e. Integer, Long etc), the object returned by valueOf() method can come from internal cache maintained by the class (e.g. java.lang.Long has a private static class LongCache which maintains cache of objects from values -128 to 127). This is done for performance sake. So, when you say
, a Long object with value 30 will be created and inserted into the cache. And the next time you create Long object with same value (i.e. 30), the same object will be returned from cache. Note that returning same object for different references is perfectly safe as all wrapper classes are immutable. The same is not true with floating point wrapper classes for various reasons. Here is a code snippet from java.lang.Long class which returns wrapper object with given long value.
Another thing to note is, when you create long object greater than 127 or less than -127 and try to do the same, you will see that you have received two different objects for the same value. Correct me if I am wrong here.