Java Basics Wrapper classes and basic types

Source: Internet
Author: User

1. The wrapper class object is immutable.

2.==, when the wrapper class and the basic type use = = comparison, the packaging class will be automatically disassembled for the basic type and then compare

3. Boolean,byte,char that is less than or equal to <=127, and short and int between -128~127 are wrapped in a fixed-address wrapper class object. But if the wrapper object is new, it will be assigned the address, is no longer fixed.

Such as:

     Public Static voidMain (string[] args) {//TODO auto-generated Method StubInteger A =NewInteger (1); Integer b=NewInteger (1); intC=1; Integer D=1; Integer e=1; System. out. println ("a==b:"+ (a==b)); System. out. println ("d==e:"+ (d==e)); System. out. println ("a==c:"+ (a==c)); System. out. println ("a==e:"+ (a==e)); System. out. println ("c==e:"+ (c==e)); The result is: a==b:falseD==e:truea==c:truea==e:falseC==e:true

4. When declaring <=128 or -128~127, a fixed wrapper class is assigned, and the method of wrapping the class integer valueOf (String s) is also assigned a fixed wrapper class object. The source code is as follows:

 Public Static Integer valueOf (int  i) {      int;       if (I >=-127//  must  cache        return Integercache.cache[i + offset];      }           return New Integer (i);      }

The Java design may be because the value of this range is the most used.

Java Basics Wrapper classes and basic types

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.