First, the basic type of Java
The Java language provides eight basic types, including six numeric types (four integers, two floating-point types), one character type, and one Boolean type.
- Integer type, including Byte, short, int, long, the default initial value is 0;
- Floating point type, including float, double, default initial value is 0.0;
- character type, char type, The default value is ", note the difference from" "," is the quotation mark of the character, "" is the quotation mark of the string, the two are different.
- Boolean, Boolean, the default initial value is False.
Note: If you do not initialize a variable of type char, you will not see any results when you export it to the console. In Java, character types are not stored in ASCII code, but are stored in a 16-bit Unicode character set, which is scoped to the character set, and the default value is u000000.
Second, packaging class
The wrapper class converts the base type to an object, and each basic type has a corresponding wrapper class in the Java.lang package, including:
Basic Type |
Package Type |
Byte |
Byte |
Short |
Short |
Int |
Integer |
Long |
Long |
Float |
Float |
Double |
Double |
Char |
Character |
Boolean |
Boolean |
In addition, there are two types used in the Java.math package for numeric calculations, namely BigInteger, BigDecimal. They have no corresponding basic types, and are mainly applied to high-precision operations, where BigInteger supports arbitrary-precision integer operations, BigDecimal supports arbitrary-precision operations with decimal points, and can be used to avoid the loss of precision resulting from the use of basic types.
Third, the use of packaging class
There are only six types of wrapper classes corresponding to the number primitives, because they are most frequently used, and the methods commonly used in these classes can be divided into two categories: one is the conversion between this type and the other base type, and the other is the conversion between the string and the type and the base type. For example, there are some common methods in the integer class:
The first five of them, all of them, belong to one, that is, the conversion to other basic types. The following three belong to the second, which is the conversion between the string and the type and the base type.
A, this type and other types of conversion
Examples are as follows:
1 New Integer (); 2 int temp2 =; 3 4 byte t1 = temp1.bytevalue (); // Conversion of wrapper classes 5 byte t2 = (byte) temp2; // coercion type conversions for basic data Types
B, this type and corresponding basic type conversion
JDK1.5 introduced the automatic boxing and unpacking mechanism, so what is boxing and unpacking?
Boxing is the conversion of the basic type into a wrapper class, divided into automatic boxing and manual boxing. Similarly, unpacking is the conversion of the package type into a basic type, also divided into automatic unpacking and manual unpacking.
Examples are as follows:
1 intA1=4;2 3 //Manual Boxing4Integer aI1 =NewInteger (A1);5 //Automatic Boxing6Integer aI2 =A1;7 8 //Manual Unpacking9 intAI1 =Ai1.intvalue ();Ten //Automatic Unpacking One intAI2 = aI2;
C, String, and basic type conversions
C1. Converting a base type to a string type
C1a. The ToString () method of the wrapper class
C1b. The valueof () method of the String class
C1c. An empty string plus a basic type variable
// There are three ways to convert a base type to a string type int b = 1; 3 String B1 = integer.tostring (b); 4 String b2 = string.valueof (b); 5 String B3 = B + ""; 6 System.out.println ("B1: " +b1+ "B2:" +b2+ "B3:" +b3 ");
C2. String conversion to base type
C2a. parse*** () static method for wrapper classes
C2b. ValueOf () method of the wrapper class
// There are two ways to convert a string into a primitive type = "121"; int C1 = Integer.parseint (b); int C2 = integer.valueof (b);
Iv. similarities and differences of basic types and packaging categories
4.1 In Java, everything is object, but the eight basic types are not objects.
4.2 The method of declaration differs, the base type is not created by the new keyword, and the wrapper type requires the New keyword.
4.3 storage methods and locations, the basic type is the value of the direct storage variables stored in the stack, can be efficiently accessed, packaging the type needs to point to the instance by reference, and the specific instance is saved in the heap.
4.4 The initial value of the wrapper type is different, and the initial value of the base type depends on the specific type.
4.5 Different ways of using, such as when working with the collection class can only use the wrapper type.
Comparison of integer and int data
In Java, there is no difference between an int and an integer type, because there is an auto-boxing/unboxing operation.
Use a program to verify the following:
Public classTestinteger { Public Static voidMain (string[] args) {intT1 = 46; intt2 = 46; Integer T3= 46; Integer T4=NewInteger (46); Integer T5=T1; Integer T6=NewInteger (T2); System.out.println ("T1 = = t2" + (T1 = =T2)); System.out.println ("T1 = = T3" + (T1 = =T3)); System.out.println ("T1 = = T4" + (T1 = =T4)); System.out.println ("T1 = = T5" + (T1 = =T5)); System.out.println ("T1 = = T6" + (T1 = =T6)); System.out.println ("T4 = = t2" + (T4 = =T2)); System.out.println ("T5 = = t2" + (T5 = =T2)); System.out.println ("T6 = = T2" + (T6 = =T2)); System.out.println ("T4 = = T3" + (T4 = =T3)); System.out.println ("T5 = = T3" + (T5 = =T3)); System.out.println ("T6 = = T3" + (T6 = =T3)); System.out.println ("T3 equals T4" +(T3.equals (T4))); System.out.println ("T3 equals T5" +(T3.equals (T5))); System.out.println ("T3 equals T6" +(T3.equals (T6))); System.out.println ("T3 equals T4" +(T3.equals (T4))); System.out.println ("T4 equals T5" +(T4.equals (T5))); System.out.println ("T4 equals T6" +(T4.equals (T6))); System.out.println ("T5 equals T6" +(T5.equals (T6))); }}
In the above program, the variables of four integer classes are defined using two boxing methods for variables and specific numbers, respectively.
In the subsequent test code, we used the = = and equals two ways to compare, the output result is:
T1 = = T2 true
T1 = = T3 true
T1 = = T4 true
T1 = = T5 true
T1 = = T6 true
T4 = = T2 true
T5 = = T2 true
T6 = = T2 true
T4 = = T3 False
T5 = = T3 true
T6 = = T3 False
T3 equals T4 True
T3 equals T5 True
T3 equals T6 True
T3 equals T4 True
T4 equals T5 True
T4 equals T6 True
T5 equals T6 True
All the results are true in a comparison that uses the Equals method. The Equals method in the integer class is specifically:
1 Public Boolean equals (Object obj) {2 if instanceof Integer) {3 return value = = ((Integer) obj). Intvalue (); 4 }5 returnfalse; 6 }
As can be seen from the code, the Equals method of the two integers is ultimately compared to whether the values of their respective wrapped intvalue are equal, so all output is true.
When using the = = comparison, two of the output results are false, which is generated when the two integer classes are compared. because when = = Compares an integer class with an int variable, the auto-boxing/unboxing handles it, and the results are compared numerically. When faced with two integer classes, = = Compares whether two references refer to the same object. In the above program, T4 and T6 are new objects created through new, so they are completely different references to the previous T3, so the result is false.
It is noteworthy that the results of the comparison between T3 and T5, the auto-boxing results for the values 46 and int variables T1 point to the same integer object, which means that in this program, when new objects are not explicitly created using new, the virtual machine simply points the reference to the result that already exists.
Java wrapper class, and comparison between integer and int