I have heard of types being referred to as "boxed" in some languages.
In Java, I have heard of "autoboxing". What is this? Is it having wrapper classes for a type? How would my code change if I'm working with boxed or unboxed types?
I have heard of types being referred to as "boxed" in some languages.
In Java, I have heard of "autoboxing". What is this? Is it having wrapper classes for a type? How would my code change if I'm working with boxed or unboxed types?
Some data types are considered "primitive", meaning they are not treated like an object and don't have the properties of an object.
On most platforms, integers and characters are examples of types that are primitive but can be boxed.
Boxing means wrapping them in an object so they have the behavior of an object.
The exact meaning and behavior depends on the language you're using. Some languages (such as Smalltalk... at least waaay back when I was doing it...) don't allow any primitive types and consider everything to be an object, but there's a performance penalty associated with that because, at the end of the day, the processor needs to work with raw numbers and raw memory to do useful work. If you want to add two integers that have been boxed, for example, behind the scenes they are "unboxed" into primitive types, the numbers are added, and they are then boxed back into a new integer.
Java の詳細情報:
オートボクシングを使用すると、ほとんどの場合、Java は や などをオブジェクト バージョンに自動的に変換できboolean
ますint
。また、その逆も可能にします。Boolean
Integer
例えば:
int a = 3; // no boxing is happening
Integer b = 3; // newer versions of java automatically convert the int 3 to Integer 3
int c = b; // these same versions also automatically convert Integer 3 to int 3
オートボクシングを持たない古いバージョンの Java では、次のコードで同じことを行う必要があります。
int a = 3; // works the same
Integer b = new Integer(3); //must set up a Integer object manually
int c = b.intValue(); //must change Integer object to a primitive
ただし、手動で行う必要があるシナリオがいくつかあります。たとえば、次のような 2 つのメソッドを持つクラスがあるとします。
assertEquals(int a, int b);
assertEquals(Object a, Object b)
今、これをやろうとすると:
Integer a = 3;
int b = 3;
assertEquals(a, b); // this will not compile
a
これが機能しない理由は、アンボックス化する必要があるのint
か、ボックス化b
する必要があるのか を判断できないためInteger
です。したがって、どのメソッド シグネチャを呼び出す必要があるかはあいまいです。これを修正するには、次のいずれかを実行できます。
assertEquals((int) a, b);
assertEquals(a, (Integer) b);
はい、ボックス化とは、値の型を取得して参照型にラップすることを意味します。Javaがオートボクシングを導入したので、次のことができます:
void foo(Object bar) {}
//...
foo(1);
また、Java は自動的に int 1 を Integer に変換します。以前のバージョンでは、次のことを行う必要がありました。
foo(new Integer(1));
ジェネリックでプリミティブを使用できないため、オートボクシングはJavaでジェネリックを使用する場合に最も役立ちます。したがって、intをリストに格納するには、を作成しList<Integer>
、intをボックス化されたリストに入れる必要があります。
A boxed type means that the values are allocated in a block on the heap and referenced through a pointer. This is good for uniformity in the implementation of the runtime (it makes it easier to have generic functions, etc), at the cost of an additional indirection.
ボックス化は、通常の値型を取り、その周りにオブジェクトを作成したことを意味します。箱に入れるようなものです。オブジェクトを構築するオーバーヘッドがあるため、これは一般的に回避する必要があります。
通常、コレクションを扱うときは、オブジェクトの配列を扱っています。Java のような言語では、プリミティブとオブジェクトの間に違いがあります。プリミティブが「ボックス化」されている場合、基本的にはプリミティブの単なるラッパーであるため、オブジェクトを期待するフレームワークの残りの部分とうまく連携します。
オートボクシングは、オブジェクトにプリミティブを配置するか、オブジェクトからプリミティブを透過的に引き出すだけの行為であるため、自分で行う余分な手順について心配する必要はありません。