Why is java.lang.Integer.value an Air Template?

Why is java.lang.Integer.value an Air Template? I tried to find the reason, but could not.

+7
source share
2 answers

If we look at the source for valueOf , we can get a hint: Source of lines java.lang.Integer : 638-643:

 public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } 

It appears that the Integer class supports the cache of Integer objects for common values. Instead of creating a new one, every time someone asks for valueOf , it simply returns a link to an existing one. Therefore, if you call Integer.valueOf(1) several times, you will return the same object each time (and not just equivalent objects).

+13
source

It looks like you were asked to solve an exercise.

If you call a method twice with the same argument, it may return the same object, thereby limiting memory usage. This is consistent with the definition of a fly.

+6
source

All Articles