翻譯 | 包裝類 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 是不可變的。當我們更改值時,它將創建新對象。

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