Java Unification-Packaging and Automatic Boxing and Unboxing

1. Basic data types

1.1 Basic types are not objects

The great unification is very important. Just like the Qin State unified the six countries, it is necessary to have the same track, the same writing, and the same philosophy. A unified system facilitates unified management.

But Java has not done it. Although it claims that everything is an object, there are actually some basic data types that are not objects. They are very famous:

  • byte
  • short
  • int
  • long
  • char
  • float
  • double
  • boolean

Although everything is an object, the embarrassment is as follows. Basic types are not objects.

	public static void main(String[] args) {
    
    
		Object obj = new Object();
		String str = new String();
		WrapperDemo wrapperDemo = new WrapperDemo();
		int num = 1;

		System.out.println(obj instanceof Object);// 输出true
		System.out.println(str instanceof Object);// 输出true
		System.out.println(wrapperDemo instanceof Object);// 输出true
		System.out.println(num instanceof Object);// 报错 Incompatible conditional operand types int and Object
	}

1.2 Unable to complete the unified operation

This is not good. If you can't provide the most basic abstraction, then there must be some methods that cannot be compatible with basic data types and reference data types at the same time.

The following method is very embarrassing, because it cannot support basic data types, so the function is like being castrated.

	/**
	 * 获取一切对象的类型名称
	 */
	public static String getObjectType(Object obj) {
    
    
		return obj.getClass().getName();
	}

2. Packaging

Java developers also found this problem, so they provided a wrapper class and defined corresponding reference types for the basic types in 8.

basic type Corresponding packaging
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean

With the wrapper class, we can unify all data types. When I define attributes and parameters, we can have an abstract foundation. As shown below, everything can be represented by objects.

public static void main(String[] args) {
    
    
		Object obj = new Object();
		String str = new String();
		WrapperDemo wrapperDemo = new WrapperDemo();
		Integer num = 1;

		System.out.println(obj instanceof Object);// 输出true
		System.out.println(str instanceof Object);// 输出true
		System.out.println(wrapperDemo instanceof Object);// 输出true
		System.out.println(num instanceof Object);// 输出true
	}

3. Automatic packing and unpacking

But everyone is still accustomed to using intthis basic type. Whoever writes in idle Integertime, there must be very few people.

It's okay. Starting from JDK 1.5, it provides the functions of automatic boxing and automatic unboxing. E.g:

	public static void main(String[] args) {
    
    
		int a = 1;
		Integer b = a;// 自动装箱:基本类型的变量-->包装类变量
		int c = b;// 自动拆箱:包装类变量-->基本类型变量
		System.out.println("a:" + a + " b:" + b + " c:" + c);// 输出 a:1 b:1 c:1
	}

Seeing whether it's boring, in fact, this realizes forward automatic compatibility. Look at the following example:

public class WrapperDemo {
    
    
	public static void main(String[] args) {
    
    
		String str = "abc";
		Integer num1 = 200;
		int num2 = 100;
		System.out.println(getObjectType(str));//输出java.lang.String
		System.out.println(getObjectType(num1));//输出java.lang.Integer
		System.out.println(getObjectType(num2));//输出java.lang.Integer
	}

	/**
	 * 获取一切对象的类型名称
	 */
	public static String getObjectType(Object obj) {
    
    
		return obj.getClass().getName();
	}
}

The type output of str and num1 is easy to understand, why does num2 output Integer.

num2 will be automatically boxed into Integer type, and Object type is the base class of all types, so it can accept Integer type parameters, so it can be printed out java.lang.Integer.

4. Summary

Through the packaging category, a global and unified perspective is provided, making everything a reality.

Through automatic packing and unpacking, the forward compatibility of the original basic types is realized and seamless connection.

This is an advancement in thinking. Through more thorough abstraction, more universal operations can be provided.

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/106955323