翻译 | 包装类 VS 基本数据类型

image.png

英语原文地址:https://sunilkanzar.wordpress.com/2017/11/11/wrapper-vs-primitive/

翻译:高行行

英文水平有限,有错误的地方欢迎指正

1. Generi 泛型

Wrapper is a reference type so you can use it as Generic [template]

包装器是一种引用类型,因此你可以将其用作泛型 [模板]

For Example :

例如 :

public class Tmp<T> {
    public Tmp() {
    }
}

If you want to create a class like That.

如果你想创建一个这样的类。

Then you can pass reference type, while creating object of it. For example

你可以传递引用类型,同时创建它的对象。例如

new Tmp<Integer>()

While you will get compile time error if you create object like :

如果你创建如下对象,则会出现编译时错误:

new Tmp<int>()

2. Avoid overloading (Some times) 避免重载(有时)

Only because of Wrapper classes it is possible to do generic datatype programming.

因为有了包装类,才有可能进行泛型数据类型编程。

For example bellow method accept any kind of number (Byte, Integer, Double, Short, Float, Long, BigDecimal, BigInteger, AtomicInteger, AtomicLong) and return the Integer addition of that numbers.

例如,以下方法接受任何类型的数字(Byte, Integer, Double, Short, Float, Long, BigDecimal, BigInteger, AtomicInteger, AtomicLong)并返回这些数字的Integer 加法。

public Integer add(Number a, Number b){
    return a.intValue() + b.intValue();
}

3. AutoBoxing and AutoUnboxing 自动装箱和自动拆箱

In earlier version of Java is not supporting AutoBoxing and AutoUnboxing. So, if You use that version of Java then you can easily differentiate the both.

在早期版本的 Java 中不支持自动装箱和自动拆箱。因此,如果你使用该版本的 Java,那么你可以轻松区分两者。

For example if you use Java 1.4 or earlier version then:

例如,如果你使用 Java 1.4 或更早版本,则:

Integer a = 1; // Auto Boxing(Not Works)
int a = new Integer(2); // Auto Unboxing(Not Works)
Integer a2 = new Integer(2); // Boxing (It Works)

4. Storage 存储

The Storage of both also differ Primitive types are stored in Stack while reference types are store in Heap

两者的存储也不同,原始类型存储在栈中,而引用类型存储在堆中

(这里说的不太对,当基本数据类型是局部变量时才存储在栈中,具体分析看这里 https://www.yuque.com/gaohanghang/sgrbwh/bsx7wv

5. Functionality 方法

You can use functionality of that class like parsing string to Integer, Double, etc and use consents of the same.

你可以使用该类的方法,例如将字符串解析为 Integer、Double 等,并使用相同的常量。

Here are the functions and consents of Integer class

以下是 Integer 类的方法和常量

image.png

6. Serialize able. 可序列化

You can serialize Integer while it is not possible with int

你可以序列化 Integer,而不能序列化 int

(int 类型是不可以序列化的。。。但是在序列化时,系统(在 JDK 1.5 采用自动装箱技术以后)会自动把 int 类型转换成其封装类型:Integer,看 Integer 类的源文件可以看出Integer类是实现了可序列化接口的,因此 Integer 是可序列化的。。。。)
—— https://bbs.csdn.net/topics/290016508

7. In RMI

You can pass Integer as a RMI method but the same is not possible with int

你可以将 Integer 作为 RMI (Java远程方法调用)方法传递,但对于 int 则不可能

Note : Both Integer and int can be part of another object in RMI argument in fact inside the Integer class they store value in int.

注意:Integer 和 int 都可以是 RMI 参数中另一个对象的一部分,实际上 Integer 类中使用 int 来存储 value 值。

如图:
image.png

8. Mutable Immutable 可变 不可变

Variable of int is mutable (It is not the case with final int) while Integer is immutable. It will create new object when we change the value.

int 的变量是可变的(final int 不是这种情况)而 Integer 是不可变的。当我们更改值时,它将创建新对象。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章