The code is as follows:
Import Java.lang.reflect.field;public class TwoPlusTwo {public static void main (string[] args) throws Exception {class CAC he = Integer.class.getDeclaredClasses () [0]; System.out.println (Cache.getname ()); Field C = Cache.getdeclaredfield ("cache"); C.setaccessible (true); integer[] array = (integer[]) c.get (cache); array[132] = array[133]; System.out.printf ("%d", 2 + 2);}}
You need to use reflection to change the cache in an integer, in the following steps:
1. Get the internal static class Integercache in integer;
2. Get the cache domain;
3. C.setaccessible (True) cancels the variable-decoration check of the JVM, although the cache array is a static final type, and we can still modify its array elements;
4. Get the array object;
5. Array range By default is 128 to 127, read source, 128 to 127, and the integer value obtained through integer.valueof is directly using the cache; cache[132] corresponds to an integer (4), It can be modified to an integer (5);
6. The function interface of the System.out.printf is printstream.printf (String format, Object ... args), since the entry is object,2 + 2 is automatically boxed, Thus output integer.valueof (4) = cache[132] = cache[133] = 5.
In Java, how to get System.out.printf ("%d", 2 + 2); output 5