Java-Let the result of the 3 become

Source: Internet
Author: User

The original source is a foreign forum in a post in the landlord question: How to let 1+1=3?
So there are the various language implementation of the various mechanisms of the answer, of course, including the direct use of the string output "1+1=3" ...
Finally, the answer is implemented in the Java language.

Here's the answer:

 Public Static void throws illegalaccessexception, nosuchfieldexception {        = Integer.  Class. getdeclaredclasses () [0];         = Cache.getdeclaredfield ("cache");        C.setaccessible (true);         = (integer[]) c.get (cache);        array[] = array[131];        System.out.printf ("%d", 1 + 1);}

  

Author explains:
You need to change it even deeper than you can typically access.
Note that this was designed for Java 6 with no funky parameters passed in on the JVM that would otherwise change the intege Rcache.
Deep within the integers class is a Flyweight of integers.
This is a array of integers from +127. CACHE[132] is the spot where 4 would normally be. Set it to 5.

That's a very clear idea.
Integer.class.getDeclaredClasses () [0] points to the Integercache class, whose cache domain is initialized within the static initialization block, and caches 128 to 127, as the cache[130]==2.

Static {    //High value is configured by property    intH = 127; String Integercachehighpropvalue=Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high"); if(Integercachehighpropvalue! =NULL) {        inti =parseint (Integercachehighpropvalue); I= Math.max (i, 127); //Maximum array size is Integer.max_valueh = math.min (i, Integer.max_value-(-Low )); } High=h; Cache=Newinteger[(high-low) + 1]; intj =Low ;  for(intk = 0; K < Cache.length; k++) Cache[k]=NewInteger (j + +);}

So write the following code, but unfortunately the result is still 2:

 Public Static void throws illegalaccessexception, nosuchfieldexception {        = Integer.  Class. getdeclaredclasses () [0];         = Cache.getdeclaredfield ("cache");        C.setaccessible (true);         = (integer[]) c.get (cache);        array[] = array[131];        System.out.println (+);}

The problem is in printf or println.

Referring to the Println method declaration, the author provides overwrite for different basic types, and printf does not provide a method for the base type.
Wkiom1p1x5uij_7faadt9pjosam217.jpg
Wkiom1p1ygerfs2naad6dtucdzm911.jpg


Although printf ("%d", plus) is equivalent to System.out.println (String.Format ("%d", 1 + 1));
But the essence of the problem is that the result of println is still the basic type and is not boxed.
That is, System.out.println (Integer), the result is 3.

Java-Let the result of the 3 become

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.