Arays.asList着信パラメータの問題
3585 ワード
次の例を見ます
Integer[] arrInteger = new Integer[10];
arrInteger[0] = 1;
arrInteger[1] = 2;
//
System.out.println(Arrays.asList(arrInteger).get(0));
//
System.out.println(Arrays.asList(1,2,3).get(0));
int[] arrInt = new int[10];
arrInt[0] = 1;
arrInt[1] = 2;
// int[] :
System.out.println(Arrays.asList(arrInt).get(0));
// int/char/short/double/boolean...
出力:1
1
[I@d542094
問題はjavaの可変パラメータタイプに出ています。Arays.asList(T...arr)なぜ可変パラメータタイプがprmitiveタイプの配列に入ると単一の要素になるのですか?
T...arrの声明はT[]arrの声明に相当し、コンパイラはcalerが入ってきたところで1つの配列を初期化し、すべての変化を行列に参加し、その後にcaleeを着用する。
范型は参照タイプだけに対して、范型Tはextensを通じて親タイプを指定しないと、デフォルトはObjectタイプであるので、この方法のバイトコード署名は、実は:
([Ljava/lang/Object;)Ljava/util/List;
prmitiveタイプが列挙されるように導入されると、Arrays.asList(1, 2, 3)
などのint型パラメータが参照タイプではないため、コンパイラがatoboxingを作成し、Integerタイプの配列を生成してasListに入るようにトリガされる。int型配列が入ってくると、配列は既に引用タイプ(タイプは「I」)であり、autbox ingは再び作用する必要がなく、直接に配列をObjectとしてasList方法に伝えます。
Java Autobox ingのトリガタイミング
Coverting a prmitive value(an int,for example)into an object of the cores ponding wrapper class(Integer)is caled toxbox ing.The Java compler atomitive value:
Object[] a = new Integer[2]; // warning,
Object[] a = new int[2]; //