logo

オートボックス化とアンボックス化:

プリミティブ データ型を同等の Wrapper 型に自動的に変換することはボックス化として知られ、その逆の操作はアンボックス化として知られています。これは Java5 の新機能です。したがって、Java プログラマは変換コードを記述する必要はありません。

オートボックス化とアンボックス化の利点:

プリミティブとラッパーの間で手動で変換する必要がないため、必要なコーディングが少なくなります。

Java でのオートボクシングの簡単な例:

 class BoxingExample1{ public static void main(String args[]){ int a=50; Integer a2=new Integer(a);//Boxing Integer a3=5;//Boxing System.out.println(a2+' '+a3); } } 
今すぐテストしてください
 Output:50 5 
この例をダウンロードする

Java でのボックス化解除の簡単な例:

ラッパー クラス型を対応するプリミティブ型に自動的に変換することは、アンボックス化として知られています。開梱の例を見てみましょう。

 class UnboxingExample1{ public static void main(String args[]){ Integer i=new Integer(50); int a=i; System.out.println(a); } } 
今すぐテストしてください

出力:

 50 

比較演算子を使用したオートボックス化とアンボックス化

オートボクシングは比較演算子を使用して実行できます。比較演算子を使用したボックス化の例を見てみましょう。
 class UnboxingExample2{ public static void main(String args[]){ Integer i=new Integer(50); if(i<100){ unboxing internally system.out.println(i); } < pre> <span> Test it Now </span> <pre> Output:50 </pre> <hr> <h3>Autoboxing and Unboxing with method overloading</h3> <table class="table"> <tr><td>In method overloading, boxing and unboxing can be performed. There are some rules for method overloading with boxing: <ul> <tr><td>Widening beats boxing</td>  </tr><tr><td>Widening beats varargs</td>  </tr><tr><td>Boxing beats varargs</td>  </tr></ul> </td></tr> </table> <h3>1) Example of Autoboxing where widening beats boxing</h3> <table class="table"> <tr><td>If there is possibility of widening and boxing, widening beats boxing.</td></tr> </table> <pre> class Boxing1{ static void m(int i){System.out.println(&apos;int&apos;);} static void m(Integer i){System.out.println(&apos;Integer&apos;);} public static void main(String args[]){ short s=30; m(s); } } </pre> <span> Test it Now </span> <pre> Output:int </pre> <hr> <h3>2) Example of Autoboxing where widening beats varargs</h3> <table class="table"> <tr><td>If there is possibility of widening and varargs, widening beats var-args.</td></tr> </table> <pre> class Boxing2{ static void m(int i, int i2){System.out.println(&apos;int int&apos;);} static void m(Integer... i){System.out.println(&apos;Integer...&apos;);} public static void main(String args[]){ short s1=30,s2=40; m(s1,s2); } } </pre> <span> Test it Now </span> <pre> Output:int int </pre> <hr> <h3>3) Example of Autoboxing where boxing beats varargs</h3> <table class="table"> <tr><td>Let&apos;s see the program where boxing beats variable argument:</td></tr> </table> <pre> class Boxing3{ static void m(Integer i){System.out.println(&apos;Integer&apos;);} static void m(Integer... i){System.out.println(&apos;Integer...&apos;);} public static void main(String args[]){ int a=30; m(a); } } </pre> <span> Test it Now </span> <pre> Output:Integer </pre> <hr> <h3>Method overloading with Widening and Boxing</h3> <table class="table"> <tr><td>Widening and Boxing can&apos;t be performed as given below:</td></tr> </table> <pre> class Boxing4{ static void m(Long l){System.out.println(&apos;Long&apos;);} public static void main(String args[]){ int a=30; m(a); } } </pre> <span> Test it Now </span> <pre> Output:Compile Time Error </pre></100){>

メソッドのオーバーロードによるオートボックス化とアンボックス化

メソッドのオーバーロードでは、ボックス化とアンボックス化を実行できます。ボックス化によるメソッドのオーバーロードには、いくつかのルールがあります。
ワイドニングビートボクシング
拡張ビートの可変引数
ボクシングが可変長に勝つ

1) ワイド化がボクシングに勝るオートボクシングの例

ワイドとボクシングの可能性がある場合は、ワイドの方がボクシングに勝ります。
 class Boxing1{ static void m(int i){System.out.println(&apos;int&apos;);} static void m(Integer i){System.out.println(&apos;Integer&apos;);} public static void main(String args[]){ short s=30; m(s); } } 
今すぐテストしてください
 Output:int 

2) ワイドニングが可変引数に勝つオートボクシングの例

拡張と可変引数の可能性がある場合は、拡張が可変引数よりも優先されます。
 class Boxing2{ static void m(int i, int i2){System.out.println(&apos;int int&apos;);} static void m(Integer... i){System.out.println(&apos;Integer...&apos;);} public static void main(String args[]){ short s1=30,s2=40; m(s1,s2); } } 
今すぐテストしてください
 Output:int int 

3) ボクシングが可変長引数に勝つオートボクシングの例

ボクシングが変数引数に勝つプログラムを見てみましょう。
 class Boxing3{ static void m(Integer i){System.out.println(&apos;Integer&apos;);} static void m(Integer... i){System.out.println(&apos;Integer...&apos;);} public static void main(String args[]){ int a=30; m(a); } } 
今すぐテストしてください
 Output:Integer 

Widening と Boxing によるメソッドのオーバーロード

以下のように、ワイドニングとボックス化は実行できません。
 class Boxing4{ static void m(Long l){System.out.println(&apos;Long&apos;);} public static void main(String args[]){ int a=30; m(a); } } 
今すぐテストしてください
 Output:Compile Time Error