Sike Java, Java data types of the ins and outs

First, the data type classification

Data types can be divided 基本数据类型(8个)and 引用数据类型(3个).
Specific reference may be as shown below:
Here Insert Picture Description

Second, the basic data types

The basic data types is, we often say that the eight major basic data types: byte、short、int、long、float、double、char、boolean
Here Insert Picture Description
Integer:

name The number of bytes occupied Occupancy digit Data representation range
byte 1 1 * 8 = 8 -27 ~ 2 7- 1
short 2 2 * 8 = 16 -215 ~ 2 15 - 1
int 4 4 * 8 = 32 -231 ~ 2 31 - 1
long 8 8 * 8 = 64 -263 ~ 2 63 - 1

Float:

name The number of bytes occupied Occupancy digit Data representation range
float 4 4 * 8 = 32 -231 ~ 2 31- 1
double 8 8 * 8 = 64 -263 ~ 2 63 - 1

Character:

name The number of bytes occupied Occupancy digit Data representation range
char 2 2 * 8 = 16 -215 ~ 2 15- 1

Boolean:

name The number of bytes occupied Occupancy digit Data representation range
boolean true and false

Third, the wrapper classes do you really know?

(A) interview questions: Java already has a basic type int, etc., why do you need packaging?

answer:
Java is an object-oriented language, all types are 引用类型.
Object class is the ancestor of all classes, that is the Object class is the parent class of all classes.
But the basic data types such as: int, double, etc. is not a reference type, nor is it inherited from Object, so int, double, etc. does not comply with object-oriented features. So Java wrapper classes need to make it with object-oriented integrity.

(Ii) a correspondence relationship with the basic data types of packaging

Basic data types and packaging correspondence table:

Basic data types Wrapper class
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double

(Iii) automatic unpacking of the packing wrapper class

1, understand it :( get int and Integer)

  • What is Unpacking: Unpacking is to be converted into Integer int;
  • What is Packing: Packing is to convert int to Integer;

2, autoboxing
example:Integer a = 100;

Equivalent to the implementation of:

Integer a = Integer.valueOf(100);

3, auto-unboxing

Integer a = 100; //装箱,实际上执行了:Integer a = Integer.valueOf(100);
int b = a;//拆箱,实际上执行了:int b = a;
System.out.println(b+1);//自动拆箱,进行运算时也会自动拆箱

4, there autoboxing performance loss, should be avoided in the circulation

Integer sum = 0;
for(int i=0; i<100; i++){
sum+=i;
}

The above codes sum+=ican be seen sum = sum + i, but the unpacking operation will first sum, and then added together before packing. Equivalent to:Integer sum = new Integer(sum.intValue() + i;);

5, comparison of the wrapper class
== symbol is a comparison reference. This comparison does not cause automatic unpacking.

Integer a=128;
Integer b=128;
System.out.println("a==b : " + (a == b)); //false,不会自动拆箱,比较的是引用,不是同一个地址引用
System.out.println("a>b : " + (a > b)); // false,会自动拆箱
System.out.println("a<b : " + (a < b)); // false,会自动拆箱

However, since the JVM caches Integer object -128 to 127, so that when the value of packaging in the range of -128 to 127, and the like are the same judgment reference of comparison.

Integer a=127;
Integer b=127;
System.out.println("a==b : " + (a == b)); //true,不会自动拆箱,比较的是引用,不是同一个地址引用
System.out.println("a>b : " + (a > b)); // false,会自动拆箱
System.out.println("a<b : " + (a < b)); // false,会自动拆箱

So for 128 such digital wrapper classes, such as the judge how to do? Very simple, unpacking manually or using the equals method.

After unpacking equals method will, according to the basic type of comparison, it is the size of both the comparison value.

Integer a=128;
Integer b=128;
System.out.println("a.intValue()==b.intValue() : " +(a.intValue()==b.intValue())); //手动拆箱,true
System.out.println("a.equals(b) : " + (a.equals(b))); //true

6, packaging type and primitive type when using == comparison will occur unboxing

Integer b=128;
int c=128;
long d=128;
System.out.println("b==c : " + (b == c)); //true ,b自动拆箱
System.out.println("b==d : " + (b == d));//true b自动拆箱,并且会自动提升类型

When the wrapper classes and basic types of comparative equals, there will be a problem.

System.out.println("b.equals(c) : " + (b.equals(c))); //true
System.out.println("b.equals(d) : " + (b.equals(d))); //false

what happened? Look at the source code equals method to know

public boolean equals(Object obj) {
if (obj instanceof Integer) {
	return value == ((Integer)obj).intValue();
}
	return false;
}

Basic types are automatically packing, into the equals method, the first step will then judge whether the other type Integer, then the long d is boxed into Long, so it is inconsistent type, direct comparison is not performed, return false. The int c will be boxed into a type Integer and consistent, can continue to compare it.

(D) packaging summary

  • Wrapper class is an object, not a primitive type.
  • Packaging and basic types can be interchangeable, the conversion process is called boxing and unboxing can be converted manually, or automatically converted.
  • Compare the size of the packaging, when there are many pits, such as:
    1, == compares reference, Integer type only in the range of -128 to 127, will hold a reference to the same.
    2, equals method will first type of comparison is consistent, inconsistent directly false.
  • Best operating practice is to compare the size of the time, unified manual before unpacking, then the comparison value.

You can refer to: the Java Why wrapper classes, how to use the wrapper class?

Published 145 original articles · won 157 Like · views 50000 +

Guess you like

Origin blog.csdn.net/qq_17623363/article/details/104914159