Concept: the basic data types (int, double, char, Boolean...). , it is convenient to use and can be used directly but there is no relevant methods can operate the basic value of the type of data, so we can use a class, the value of the type of these basic data packaged,
/* *
Packing:
The basic value of the type of data (int, double, char, Boolean... ), it is convenient to use, can directly use the
But there were no related methods, can operate on these basic data type values
So we can use a class, the value of the type of these basic data packaged
Some methods in the class definition for operating the basic value of the type of data
This class is called a wrapper class
4 kinds of 8 kinds of basic data types:
Byte short int, long, float double char Boolean
Basic types of corresponding wrapper class: Java lang package
Byte Short Integer Long Float Double Character Boolean
Java. Lang. Integer class
Integer class packaging in the object the basic value of the int type a,
*/
Public class Demo01Integer {
Public static void main (String [] args) {
/* *
Packing: the value of the basic types of packing to the class of int - & gt; The Integer
The structure of the wrapper class methods:
Integer (int value) passed Integer
Integer (String s) passed a String type Integer
Wrapper class static method:
The static Integer the valueOf (int I) pass Integer
The static Integer the valueOf (String s) passed a String type Integer
Note:
Two string method, must pass an integer type string, or throws an exception
*/
The Integer in1=new Integer (10);
System. The out. Println (in1);//10 override the toString method
The Integer in2=new Integer (" 10 ");
System. The out. Println (in2);//10 override the toString method
The Integer in3=Integer. The valueOf (10);
System. The out. Println (in3);//10
The Integer in4=Integer. The valueOf (" 10 ");
System. The out. Println (in4);//10
//Integer in5=Integer. The valueOf (" a ");//a NumberFormatException (digital format) : For input string: "a"
//System. Out. Println (in5);
Double d1=Double. The valueOf (5.5);
System. The out. Println (d1);//5.5
/* *
Split open a case: in a wrapper class to retrieve values of basic types Integer - & gt; Int
Method in the Integer class:
Int intValue () returns the Integer value to int type,
*/
Int a=in1. IntValue ();
System.out.println(a);//10
}
}
automatic packing, devanning
/* *
Automatic packing, devanning, after JDK1.5, packing and unpacking can automatically (values of basic types and values can be automatically converted to wrapper classes)
Packing: basic types - & gt; A wrapper class
Split open a case: the wrapper classes -- & gt; Basic types of
*/
Public class Demo02Integer {
Public static void main (String [] args) {
/* *
Automatic packing:
The Integer in=10;
The implied a process of creating objects
The Integer in=new Integer (10);
*/
The Integer in=10;
/* *
Automatic unpacking:
In is a wrapper class, cannot be directly involved in the computation
In the default will call intValue method, the wrapper classes into basic types of integer and 1 in addition
In the intValue () + 1===11 & gt; The result is a basic type of integer, and assign a value to the in the wrapper class (automatic packing)
*/
In=in + 1;
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
ArrayListList=new ArrayList<> (a);
List. The add (1);//automatic packing is equivalent to the list. The add (new Intger (1));
//int a=list. Get (0). IntValue ();
Int a=list. Get (0);//automatic unpacking the get method to obtain the element is an Integer type, the default call intValue method for conversion to type int value
}
}
3. Basic data types and string conversion between
/* *
Basic types and string conversion between
1. The basic data types - & gt; String
A. the value of the basic data type + ":" work is one of the most commonly used 1 + "==" & gt;" 1
"B. use the static methods in the wrapper class toString:
The static String toString (int I) returns a specified integer String objects,
C. use the String class static methods in the valueOf:
The static String the valueOf (int I) returns a String representation of the int parameter form,
2. The string - & gt; The basic data type
In each of the wrapper class has a parseXXX method, can put the string format of the basic types of data, converted to basic types of number
The Integer types: static int parseInt (String s)
Double categories: static Double parseDouble (String s)
.
Note:
String must pass the basic types of strings, or sell digital formatting anomaly
*/
Public class Demo03Integer {
Public static void main (String [] args) {
//1. Basic data types -- -- & gt; String
Basic data type value +//a. "" : the most commonly used in 1 +"=="& gt;" 1
"String s1=1 + ""
System. The out. Println (s1 + 10);//110
//b. use the static methods in the wrapper class toString:
String s2=Integer. ToString (10);
System. The out. Println (s2 + 10);//1010
//c. use the String class static methods in the valueOf:
String s3=String. The valueOf (100);
System. The out. Println (s3 + 10);//10010
//2. String -- -- & gt; The basic data type
Int a=Integer. ParseInt (" 100 ");
System. The out. Println (a + 100);//200
Double d=double. ParseDouble (" 1.1 ");
System. The out. Println (d + 2.2);//3.3000000000000003
//int ABC=Integer. ParseInt (" ABC ");//a NumberFormatException: For input string: "ABC", "
//System. Out. Println (ABC);
}
}