Introduction of basic type wrapper type
1. Overview
The basic type is what we mentioned earlier, such as int, String, float and other commonly used data types, these are basic data types, in order to do more basic type data operation
, and more convenient operations, Java provides the appropriate class type for each data type, that is, the wrapper type.
2. Correspondence relationship
Byte (Byte), short (short), int (Integer), long (long), float (float), double (double), char (Character), Boolean (Boolean)
3. Detailed
(1). How to construct an integer class
public Integer (int value)
Public Integer (String str)
(2). The member method of the integer
String-->int
string.valueof (number);
int--->string
Integer.parseint (str)
(3). Conversion of integer into binary
public static String tobinarystring (int i);//Decimal to two-level system
public static String tooctalstring (int i);//Decimal to eight-level system
public static String tohexstring (int i);//Decimal to 12-level system
(4). Automatic unboxing of JDK5
Automatic Boxing: Converting basic types to package types
Automatic unpacking: Converting package types to basic types
In the auto-unpacking box, you need to pay attention to the situation of NULL pointers.
code example:
public class Test {
public static void Main (string[] args) {
int i=10;
String str= "100";
Construction method test for integer
Integer TG =new integer (i);
SYSTEM.OUT.PRINTLN ("Test is:" +TG);
Integer TG1 =new integer (str);
SYSTEM.OUT.PRINTLN ("Test is:" +str+ "---" +TG1);
System.out.println ("-----------------------");
Mutual conversions between int and string types
String--->int
int Getvalue=integer.parseint (str);
System.out.println ("Test1 is:" +getvalue);
int--->string
String getstr=i+ "";
String getstr1=string.valueof (i);
System.out.println ("Test1 is:" +getstr+ "---" +getstr1);
System.out.println ("-----------------------");
In-Process conversion testing
System.out.println ("Test2 10-->2:" +integer.tobinarystring (i));
System.out.println ("Test2 10-->8:" +integer.tooctalstring (i));
System.out.println ("Test2 10-->12:" +integer.tohexstring (i));
System.out.println ("-----------------------");
Automatic Disassembly test
Integer t=100;
t+=20;
System.out.println ("I:" +t);
Source
/*integer t=integer.valueof (100);
T=integer.valueof (T.intvalue () +20); * *
}
}
Output Result:
Test Is:10
Test is:100---100
-----------------------
Test1 is:100
Test1 Is:10---10
-----------------------
Test2 10-->2:1010
Test2 10-->8:12
Test2 10-->12:a
-----------------------
i:120
(5). The question of the related face of integer
A:integer i=1;i+=1;
Solution: Integer I=1: Base type to reference type, auto boxing
i+=1; Reference type to basic type, first automatically unpacking, and then automatically boxed
B: Look at the program to write the results:
public class Test {
public static void Main (string[] args) {
Integer i1=new integer (127);
Integer i2=new integer (127);
System.out.println (I1==I2);
System.out.println (I1.equals (I2));
System.out.println ("-----------");
Integer i3=new integer (127);
Integer i4=new integer (127);
System.out.println (I3==I4);
System.out.println (I3.equals (I4));
System.out.println ("************************************8");
Integer i5=128;
Integer i6=128;
System.out.println (I5==I6);
System.out.println (I5.equals (I6));
System.out.println ("-----------");
Integer i7=127;
Integer i8=127;
System.out.println (I7==I8);
System.out.println (I7.equals (i8));
System.out.println ("-----------");
}
}
Output Result:
False
True
-----------
False
True
************************************
False
True
-----------
True
True
-----------
Analysis:
A: Because Equals is the value that exists inside, it is true
B: Through the source code we know, conquer your -128~127 data, do a data buffer pool, if it is within the range of data, the call will not create an object.
If it is exceeded, the object is created.
Java Basics-Introduction to basic types of packaging (1)