Java-Witty to make the result of the 3

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 Main (string[] args) throws Illegalaccessexception, nosuchfieldexception {Class cache = Integer.        Class.getdeclaredclasses () [0];        Field C = Cache.getdeclaredfield ("cache");        C.setaccessible (TRUE);        integer[] Array = (integer[]) c.get (cache);        ARRAY[130] = 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 may be configured by property             int h = 127;             String integerCacheHighPropValue =                  Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high");             if  (integercachehighpropvalue != null)  {                 int i = parseint ( Integercachehighpropvalue);                 i = math.max (I,&Nbsp;127);                 //  Maximum array size is Integer.MAX_VALUE                 h = math.min (i, Integer.MAX_VALUE -   (-low));            }             high = h;             cache = new integer[(High - low)  + 1];             int j = low;             for (int k = 0; k  < cache.length; k++)                  cache[k]&nbsP;= new integer (j + +);         } 



See here and suddenly, almost use Java for a period of time people know this, so write the following code, but unfortunately the result is still 2:

public static void Main (string[] args) throws Illegalaccessexception, nosuchfieldexception {Class cache = Integer.        Class.getdeclaredclasses () [0];        Field C = Cache.getdeclaredfield ("cache");        C.setaccessible (TRUE);        integer[] Array = (integer[]) c.get (cache);        ARRAY[130] = 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.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/47/09/wKiom1P1x5uiJ_7FAADt9PjOSaM217.jpg "title=" Println.jpg "alt=" Wkiom1p1x5uij_7faadt9pjosam217.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/47/09/wKiom1P1yGeRfS2nAAD6dtUCDzM911.jpg "title=" Printf.jpg "alt=" 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.



This article is from the Alvez. -99.9% 0b/s "blog, be sure to keep this source http://alvez.blog.51cto.com/7711135/1543281

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.