Java Knowledge Point Grooming--boxing and unpacking

Source: Internet
Author: User
Tags wrapper

1. Preface: Java is a typical object-oriented programming language, but there are 8 basic data types that do not support object-oriented programming, the basic data types do not have the characteristics of objects, no properties and methods ; Java has designed corresponding classes for this 8 basic data types ( wrapper classes ), so that they convert to each other, indirect implementation of basic data types with object characteristics, enrich the basic data type operation;

Basic data types Packing class
Byte Byte
Short Short
Int Integer
Long Long
Char Character
Float Float
Double Double
Boolean Boolean





Note: int is the base quantity type, Integer is class, int and Ingeger initial values are different (int default value 0 ingeger default null), Integer contains properties and methods, int does not The process of converting basic data types and wrapper classes is called boxing and unpacking;

2, Boxing: The basic data type conversion to the corresponding packaging class (Int-->integer);

1 New // Manual Boxing 2 // Automatic Boxing

3, Unpacking: Packaging class conversion into basic data type (integer-->int);

1 // Automatic Boxing 2 int // Automatic Unpacking 3 int // Manual Unpacking

4, automatic packing and unpacking: Java 1.5 Only after the automatic boxing, unpacking features, automatic boxing is through the packaging class valueof method implementation, automatic unpacking is through the packaging class Xxxvalue method to achieve ;

1 // call the ValueOf method to auto-boxing 2 int // call the Intvalue method to automatically remove the box

Note: Auto-boxing implicitly creates objects, paying attention to the automatic boxing problem in a loop, which affects program performance.

5, equals and = = Comparison: when the two operands of the "= =" operator are wrapper classes, whether the comparison points to the same object, and if one of the operands is an expression (that is, contains arithmetic operations) The comparison is a numeric value (triggering the automatic unpacking) , for the wrapper class, The Equals method does not convert the type ;

1  Public classMain {2      Public Static voidMain (string[] args) {3          4Integer A = 1;5Integer B = 2;6Integer C = 3;7Integer d = 3;8Integer e = 321;9Integer f = 321;TenLong g = 3L; OneLong h = 2L; A           -System.out.println (C==d);//true -System.out.println (E==F);//false theSystem.out.println (c== (a+b));//true -System.out.println (C.equals (a+b));//true -System.out.println (g== (a+b));//true -System.out.println (G.equals (a+b));//false +System.out.println (G.equals (a+h));//true -     } +}

parsing: The JVM caches 128 to 127 integer objects (short, Byte, Character, Long and integer similar), so the first one is the same object true, the second is the new object false, and the third has a + The method compares the values of true on both sides, the fourth has a + method operation and the data type of the two sides is the same true; the fifth is the same as four first, the + method sum, = = will be type conversion (Long) true; Sixth first + method summation, equals does not type conversion false; Seventh First + The sum of the methods (triggering the unboxing, into the int type and the long type, which triggers the type promotion, the result is a long type) equals when the comparison triggers a boxing comparison on both sides is a long type result true;

Java Knowledge Point Grooming--boxing and unpacking

Related Article

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.