Java wrapper class

Source: Internet
Author: User
Tags wrapper

First of all, why is there a packaging class? Java is an object-oriented programming language, and the basic type does not have the nature of the object, although Java can handle the basic type directly, but sometimes it needs to be treated as an object, which requires changing the base type to the wrapper type.

The wrapper type for the eight basic data types in Java.

Basic data type Wrapper type

BYTE java.lang.Byte

Short Java.lang.Short

int Java.lang.Integer

Long Java.lang.Long

Float Java.lang.Float

Double java.lang.Double

Boolean Java.lang.Boolean

Char Java.lang.Character

First look at the inheritance of the wrapper class, such as:

Combine the following code to see why the wrapper type appears in Java.

Requirement: Specifies that the M1 method can accept any one of the data types in Java
PublicClassintegertest01{
Public static  void    M1 ( Object o) {  
   system. Out.println (o);
 }
  public  static   void  Main ( string[] args) {
    //Basic data type
    byte b= ten;
    //reference data type
   byte b1= new byte (b);
   M1 (B1);    //10  byte has overridden the ToString method in Object
 }
}

The above code defines a M1 () method whose parameter type is the object class, which is the reference data type, and if the M1 method wants to accept data of byte type (the data of byte type is the basic data type), you can first wrap the data of byte type into Java.lang.Byte. Re-pass parameters.

The following is an example of a java.lang.Integer type, which explains eight types, which means that the eight types of methods are generic.

PublicClassintegertest02{
PublicStaticvoidMainString[] (args) {
Gets the maximum and minimum values of type int
System.Out.println ("The maximum value of type int is:" +integer.max_value);
System.Out.println ("The maximum value of type int is:" +integer.min_value);
Push byte with int
System.Out.println (The maximum value of the Byte type is: "+byte.max_value);
   system. The maximum value for the out.println ( "Byte type is:" +byte.min_value);
    //Create an object of type Integer
   integer i1= new  integer ();    //convert int type to Integer type
   integer i2= new integer ( "123");   // Converts a string type to an integer type   (although the string type can be converted to an integer type, but the string must also be a number)
     integer i3= new integer ( "abc");     //Compile pass, run times exception: NumberFormatException
    system. Out.println (I1);
   system. out.println (I2);
 }
} /span>

Take a look at the commonly used methods in integer.

/*
Common methods in integer
*/
PublicClassintegertest03{
PublicStaticvoidMainString[] (args) {
Integer i1=NewInteger (10);Conversion of int type to integer type basic data type to reference data type
System.Out.println (I1);10
IntI2=i1.intvalue ();Convert an integer type to an int type
System.Out.println (I2);10

static int parseint (string s) string type converted to int type
IntI3=integer.parseint ("123");
System.Out.println (i3+1);124
int I4=integer.parseint ("abc"); Error: Java.lang.numberformatexception,string type converted to string, this string must be a numeric string
System.out.println (I4);
Static Double parsedouble (String s)
Convert string type to double type
DoubleI5=double.parsedouble ("2234.342");
System.Out.println (i5+22.23);2256.572
Converts a decimal of type int to a binary
String s1=integer.tobinarystring (10);1010
System.Out.println (S1);
//convert decimal of type int to octal
String s2=integer.tooctalstring (10); //12
System. out.println (S2);
//convert decimal of type int to hexadecimal
String s3=integer.tohexstring (10); //a
System. Out.println (S3);
//int--->integer
Integer s4=integer.valueof (10);
System.              Out.println (S4); //10
//string--->integer
Integer s5=integer.valueof ("10");
System.            Out.println (S5); //10
}
}

Integer,int,string three types are converted to each other.

/*
Integer
Int
String
Three types convert each other
*/
PublicClassintegertest04{
PublicStaticvoidMainString[] (args) {
Int-->integer
Integer i1=integer.valueof (10);
System.Out.println (I1);10
Integer-->int
IntI2=i1.intvalue ();
System.Out.println (I2);10
String-->integer
Integer i3=integer.valueof ("21131");
System.OUT.PRINTLN (i3);//21131
    //integer-->string
   string i6=integer.tostring (i3);  < Span class= "Apple-converted-space" >&NBSP;
   system. Out.println (I6);                &NBSP; //211131
   string i4=i3.tostring ();
   system. Out.println (I4)                 //21131
    // String-->int
    int i8=integer.parseint ( " 23 ");
   system. out.println (i8);            &NBSP; //23
    //int-->string
   string i7= + + "";
 }
}

With the basis of the above content, we can learn what is called automatic unpacking and automatic packing.

1. Automatic boxing and automatic unpacking is a concept in the program compilation phase, which is not related to program operation.

2. The main purpose of automatic boxing and automatic unpacking is to facilitate the programmer's call

PublicClassintegertest05{
PublicStaticvoidMainString[] (args) {
Previous versions of JDK5.0
Integer i1=New Integer (10); //int-->integer
int i2=i1.intvalue (); //integer-->int
//Automatic Boxing
Integer i3=10;
//Automatic unpacking
int I4=i3;
M1 (10); //Automatic Boxing
}
Public static void M1 (Object o) {
System. out.println (o);
}

}

Take a closer look at automatic unpacking and automatic boxing with the following code.

PublicClassintegertest06{
PublicStaticvoidMainString[] (args) {
Integer i1=NewInteger (10);
Integer i2=NewInteger (10);
There's no automatic unpacking here.
System.Out.println (I1==I2);False
Compares data of two integer types for equality and cannot be used = =
Integer has overridden the ToString method in object
System.Out.println (I1.Equals (I2));True
Note the following procedures
If the data is between [ -128~127], a "integer constant Pool" is introduced in Java, in the method area.
The integer constant pool stores only data between -128~127.
Integer i3=128;
Integer i4=128;
The above is equivalent to
Integer i3=new integer (128);
Integer i4=new integer (128);
System.Out.println (I3==I4);False
String s1="ABC";
   string s2= "ABC";
   system. out.println (S1==S2);     //true
         integer i5= 127;       //This program will not create objects in the heap and will be taken directly from the integer constant pool
                integer i6= 127;
               system. out.println (I5==I6);     //true
   integer i7= -128;
   integer i8= -128;
   system. out.println (i7==i8);    &NBSP; //true
    Integer i9= -129;
   integer i10= -129;
   system. out.println (i9==i10);    &NBSP; //false
    }
}

Integer i1=new integer (10);

Integer i2=new integer (10);

System.out.println (I1==I2);

It is important to note that I1 and i2 are integer types, there is no auto-unpacking, so comparing data of two integer types is equal, cannot be used = =, is consistent with the previous comparison method of string type, and requires the Equals () method. Next, if the data is between [ -128~127], a "integer constant Pool" is introduced in Java, and in the method area, the integer constant pool stores only data between -128~127. This is why the integer I5=127;integer i6=127; (these two sentences are equivalent to the integer i5=new integer (127); integer i6=new integer (127); ) System.out.println (I5==I6); The result is true while the Integer I3=128;integer i4=128; System.out.println (I3==I4); The result is false. The equivalent of the former is a string compared with "= =", while the latter compares the memory address.

Java wrapper 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.