Caching mechanism of Java Integer Auto-boxing

Source: Internet
Author: User

See Code validation

/*** Integer caching mechanism * The following caches are only useful when auto-boxing (Autobox). New object is useless*/ Public classIntegertypecache { Public Static voidMain (string[] args) {/*** Run log when greater than 127, the Autobox reference is judged to be false*/         for(inti = 0; I < 200; i++) {            /*** Integer Cache Test * * Caches Default-128 to 127, range may be modified by-xx:autoboxcachemax=size */Integer Integer1=NewInteger (i); Integer Integer2=NewInteger (i); Integer Integer3=i; Integer Integer4=i; BooleanRESULT1 = Integer1 = =Integer2; BooleanRESULT2 = Integer3 = =integer4; System.out.println ("Integer1" + integer1 + "= integer2" + integer2 + "result is" +RESULT1); System.out.println ("Integer3" + integer3 + "= integer4" + integer4 + "result is" +result2); /*** Long Cache Test * Cache range from-128 to 127*/Long long1=NewLong (i); Long long2=NewLong (i); Integer Long3=i; Integer Long4=i; BooleanRESULT3 = Long1 = =long2; BooleanRESULT4 = Long3 = =Long4; System.out.println ("Long1" + long1 + "= long2" + long2 + "result is" +RESULT3); System.out.println ("Long3" + long3 + "= Long4" + Long4 + "result is" +RESULT4); /***short Cache Test * Caches range from 128 to 127*/Short Short1=NewShort (( Short) (i); Short Short2=NewShort (( Short) (i); Short Short3= ( Short) I; Short Short4= ( Short) I; BooleanRESULT5 = Short1 = =Short2; BooleanResult6 = Short3 = =Short4; System.out.println ("Short1" + short1 + "= short2" + short2 + "result is" +RESULT5); System.out.println ("Short3" + short3 + "= short4" + short4 + "result is" +result6);        System.out.println (); }        /*** Character Cache size is Unicode 0-127*/Character Character1=NewCharacter (' 1 ');//UnicodeCharacter character2 = ' 1 '; Character Character3= ' 1 '; System.out.println ("Character1 = = Character2" + (Character1 = = Character2));//falseSystem.out.println ("Character3 = = character4" + (Character2 = = Character3));//trueCharacter character4=NewCharacter (' big ');//unicode>127Character Character7 =NewCharacter (' big ')); Character Character5= ' big '; Character Character6= ' big '; System.out.println ("Character4 = = Character5" + (character4 = = Character5));//falseSystem.out.println ("Character5 = = Character6" + (Character5 = = Character6));//falseSystem.out.println ("character4 = = Character7" + (character4 = = Character7));//false        /*** Byte Cache size is 128 to 127 * Autobox Reference comparison is all true*/         for(bytei = Byte.min_value; i < Byte.max_value; i++) {Byte byte1=NewByte (i); Byte Byte2=NewByte (i); Byte Byte3=i; Byte byte4=i; System.out.println ("Byte1" + byte1 + "= = Byte2" + Byte2 + "is" + (byte1 = Byte2));//falseSystem.out.println ("Byte3" + byte3 + "= = Byte4" + byte4 + "is" + (Byte3 = = byte4));//trueSystem.out.println (); }    }}

Caching mechanism of Java Integer Auto-boxing

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.