Javase Basic Packaging class

Source: Internet
Author: User

I. Concept:

The 8 basic types of the Java language correspond to 8 kinds of "wrapper classes" respectively. Each wrapper class encapsulates a corresponding base type member variable and also provides a practical method for that data type.
1) Purpose of wrapper class: Used to treat basic type data as reference type.
2) The name of the wrapper class: In addition to the integer (int), Character (char), the rest of the wrapper class names are capitalized in the first letter of the base type name.
3) Disassembly, Boxing: Integer i=new integer (1); Creates an integer 1 that exists as an object, and the process of converting from a base type to a reference type is called "Boxing", and vice versa, "unpacking."
4) Boxing: Mode one: Double d=new double (2.2);//Boxing
mode two: Double d=double.valueof (2.2);//Basic types have ValueOf method
5) Unboxing: Double Num=d.doublevalue ();//Unpacking
6) Packing class use premise: jdk1.5+
Public static void Say (Object obj) {System.out.println (obj); }
int a=1;//Basic type, not object subclass!
say (a);//in Java version 1.4, this is a syntax error! Because int is a basic type, not an object, you should write your own method for 8 basic types
7) Use of wrapper classes: Instantiate an object that represents the function of an integer 1;integer to make the basic type int look like a reference type. This allows you to participate in object-oriented programming. Thus we can treat an int as an object and become a subclass of object.
integer i=new integer (a);//boxing, or write integer i=new integer (1);
Integer ii=integer.valueof (a);//Boxing Another way
int Num=i.intvalue ();//unboxing say (i);//integer is a subclass of object that can be called!
8) JDK1.5 Packaging class automatic Disassembly box (principle):

when compiling the source program, the compiler will preprocess and automatically unboxing and boxing statements that are not unboxing and boxing. Can be found by the anti-compiler.
Say (Integer.valueof (a)); Automatic boxing num=i;//reference type variable how can I copy it to the base type?
//num=i.intvaluse ();//Automatic unpacking
9) Some common functions of wrapper classes: Convert a string to its type by: Parsexxx,xxx represents its type. Pay special attention here! Make sure that the string to be converted is really or compatible with the data type you want to convert! Otherwise, an exception will be thrown!
String numstr= "123"; System.out.println (numstr+1);//1231
int Num=integer.parseint (NUMSTR); System.out.println (num+1)//124
long Longnum=long.parselong (NUMSTR); System.out.println (longnum);//123
double doublenum=double.parsedouble (NUMSTR); System.out.println (doublenum);//123.0
) integer provides several interesting ways to:

Converts an integer to a 16-binary form, returns it as a string, converts an integer to a 2-binary form, and returns it as a string.
String bstr=integer.tobinarystring (num); String hstr=integer.tohexstring (num);
11) All packaging classes have several common: get the maximum and minimum values.
int max=integer.max_value;//int Max int min=integer.min_value;//int min value
System.out.println (integer.tobinarystring (max)); System.out.println (integer.tobinarystring (min));

two. Code
/*** Wrapper class * Basic type consists of 8, since they are present in the form of a value, no inheritance * nor reference type, so do not inherit from object. * Then the basic types are not involved in object-oriented development. * The role of the wrapper class mainly solves the problem that the basic type cannot participate in object-oriented development *. * * 6 of the wrapper classes that represent numbers are all inherited from the number class * Number class defined: * int intvalue () * Returns the number of the current object represented by an int value of the base type * * Double doublevalue () * Represents the current object The word has a basic type of double value returned * ... 6 methods of numeric type all-in-one * **/ Public classIntegerdemo { Public Static voidMain (string[] args) {//the object represents a decimal 1.2Double Doub =NewDouble (1.2); DoubleD =Doub.doublevalue (); System.out.println (d);//1.2//you can also convert to other basic type data, but you may lose precision        inti =Doub.intvalue (); System.out.println (i);//1    }}/*** The number wrapper class has two constants, representing the corresponding base type * Data range: * min_value * max_value **/ Public classIntegerDemo2 { Public Static voidMain (string[] args) {intMax =Integer.max_value; intMin =Integer.min_value;        SYSTEM.OUT.PRINTLN (max);    System.out.println (min); }}/*** Packaging Class one of the most commonly used methods * The Digital wrapper class has a static method: Parsexxx () * can convert a string to the corresponding base type *@authorAdministrator **/ Public classIntegerDemo3 { Public Static voidMain (string[] args) {String intstr= "123"; /** If you want to convert the string to the corresponding number, if the string * describes all the characters must represent the current type of number to be able! */        inti =Integer.parseint (INTSTR); System.out.println (i+1); DoubleD =double.parsedouble (INTSTR);    System.out.println (d); }} Public classIntegerDemo4 { Public Static voidMain (string[] args) {Dosome (1); /** Java has introduced a new feature since 1.5 * auto-unpacking * With this feature, we do not have to be special when writing a program * Notice the basic type and the corresponding packaging class of the problem of mutual transfer. * But this is not a JVM endorsement.         Instead, the compiler recognizes the reason that the compiler will convert the code to help us complete and then write to the bytecode * file. */        /** The Auto-boxing feature is used here. * The compiler changed the code when it generated the bytecode file: * Integer inte = integer.valueof (1); */Integer inte= 1; /** The automatic unpacking feature is used here. * When the compiler generates a bytecode file, it changes the code to: * int i = Inte.intvalue (); */        inti =inte; }     Public Static voiddosome (Object o) {}}/*** static method ValueOf () is recommended when converting a basic type to a wrapper class *@authorAdministrator **/ Public classIntegerDemo5 { Public Static voidMain (string[] args) {/** valueof objects are reused in some wrapper classes * such as Integer, which caches 1-byte integers. -Between 128 and 127*/Integer I1= integer.valueof (1); Integer I2= integer.valueof (1); System.out.println (I1==i2);    System.out.println (I1.equals (I2)); }}

Javase Basic Packaging class

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.