Good job!
Dim r As Reflector
#region Secret
Dim cache() As Object = r.GetStaticField("java.lang.Integer$IntegerCache", "cache")
cache(128+14) = 3
#end region
That's the correct answer. Only 2 lines are needed (I corrected it in post #18).
Explanation:
For performance reasons, each numeric type is represented with two types: the primitive type (Java int) and the object type (Java Integer).
There are many cases where a primitive type needs to be converted to the object type and vice versa. This conversion is called boxing.
Dim i As Int = 4 'primitive
Dim o As Object = 4 ' primitive converted to object
i = o 'object converted to primitive
List.Add(17) 'List holds objects so it will hold the object type - primitive converted to object
The compiler takes care of these conversions automatically.
As an optimization the JVM holds a small cache (array) that holds the object types of values between -128 to 127.
This is the relevant code:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
The reflection code gets this cache and puts the object type with the value 3 in the place of the object type with the value 14.
The result is:
Dim o As Object = 14 'The primitive type is converted to an object type using the small cache.
Log(o) '3