Because classrooms are learning Java, Java also new to distance himself last year, so it is just as a refresher, I wrote this note (live update)
java-based finishing notes
The Java wrapper classes and primitive types
I. Introduction and basic types of packaging
Eight basic types (also known as primitive types): byte, short, int, long, float, double, char, boolean
corresponding eight wrapper class: Byte, Short, Integer, Long , Float, Double, Character, Boolean
Second, the automatic conversion and cast
1, the principle of automatic conversion (from low to high precision accuracy): byte, short, char (peer) -> int -> long -> float -> double
byte b = 1;
char c = 1;
short s = 1;
int i = 1;
c = b; // Error,同级
c = s; // Error,同级
s = c; // Error,同级
i = c; // OK
int i = 1;
long t = 1;
float f = 1;
double d = 1;
f = i; // Ok
f = t; // Ok
d = f; // Ok
2, cast
cast formats are needed before the data transformation plus "()", followed by addition of the data types need to be converted in parentheses
byte b = 3;
int i = 3;
long t = 3;
float f = 3;
char c = 3;
short s = 3;
i = (int) f; // OK,由高精度向低精度转换
t = (long) f; // OK,由高精度向低精度转换
b = (byte) i; // OK,由高精度向低精度转换
i = b; // OK,由低精度向高精度转换,自动转型
System.out.println(c==s); // OK,true,c 和 s 自动转型为int,然后比较
b = (byte) s; // OK,一种类型到另一种类型转换
c = (char) b; // OK,一种类型到另一种类型转换
c = (char) s; // OK,一种类型到另一种类型转换
Third, the automatic boxing and unboxing
1, the basic type into a packaging
method: can be done by the corresponding wrapper class constructor
Method two: the wrapper class provide a static function
package august;
public class IntegerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义一个double类型的数据
double d=123.12;
//1.通过构造函数把基本数据类型转换为对应的包装类型
Double d1 = new Double(d);
System.out.println(d1); //123.12
//2.使用包装类中的静态方法来完成
Double d2 = Double.valueOf(d);
System.out.println(d2); //123.12
}
}
2, the package type is converted to the basic types
can use the functions provided in the wrapper class: xxxValue, (xxx represents the type to be converted)
3, autoboxing : basic type is automatically converted to the type of packaging. Unpacking : that automatically converts package type as the basic type.
Before Java SE5, if you are packing, necessary first to create a wrapper class object, and after Java SE5, need not do so. Directly like this:
Integer i = 10; //装箱
int n = i; //拆箱
Principle :It called automatically when the packing is of Integer valueOf (int) method. Automatically invoked When unpacking is Integer method of intValue. For other wrapper classes, such as Double, Character, also apply.
* But beware: If the integer literal between [-128,127], they will not directly create objects returned, the rest of the city () method to re-create objects valueOf. Look at the code
public class Main {
public static void main(String[] args) {
Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 200;
Integer i4 = 200;
System.out.println(i1==i2); // true因为100在[-128,127]之间,所以没有重新创建对象,返回值为true
System.out.println(i3==i4); // false因为200不在此区间,所以两者均重新创建了新的对象,所以返回值为false
}
}
public class Main {
public static void main(String[] args) {
Double i1 = 100.0;
Double i2 = 100.0;
Double i3 = 200.0;
Double i4 = 200.0;
System.out.println(i1==i2); // false
System.out.println(i3==i4); // false
//四者均为double类型的值,直接都创建新的对象
}
}
Summary:
. 1, Integer, Short, Byte, Character, Long valueOf achieve these classes are similar method, include the limited, shared [-128,127];
2, Double, Float achieve valueOf method are similar, Unlimited not include, not shared;
3, to achieve the Boolean valueOf method other than the above floating-point and integer, only two values, include limited, shared;
Further, Integer i = new Integer (xxx); does not trigger automatic packing. The Integer i = xxx; triggers an automatic packing; resource efficiency and the second way is occupied in the general case ([-128,127]) is better than the first case (note that this is not absolute).
Details Reference: https://blog.csdn.net/justloveyou_/article/details/52651211 and https://www.jianshu.com/p/f00b3e5d3142