logo

التشغيل الآلي والفتح:

يُعرف التحويل التلقائي لأنواع البيانات البدائية إلى نوع الغلاف المكافئ لها باسم 'الملاكمة' وتُعرف العملية المعاكسة باسم 'unboxing'. هذه هي الميزة الجديدة لـ Java5. لذلك لا يحتاج مبرمج جافا إلى كتابة رمز التحويل.

الاستفادة من Autoboxing وUnboxing:

ليست هناك حاجة للتحويل بين البدائيات والأغلفة يدويًا، لذلك يلزم ترميز أقل.

مثال بسيط على Autoboxing في جافا:

 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 
قم بتنزيل هذا المثال

مثال بسيط لUnboxing في جافا:

يُعرف التحويل التلقائي لنوع فئة الغلاف إلى النوع البدائي المقابل باسم Unboxing. دعونا نرى مثال فتح العلبة:

 class UnboxingExample1{ public static void main(String args[]){ Integer i=new Integer(50); int a=i; System.out.println(a); } } 
اختبره الآن

انتاج:

 50 

Autoboxing وUnboxing مع عوامل المقارنة

يمكن إجراء عملية Autoboxing باستخدام عوامل المقارنة. دعونا نرى مثال الملاكمة مع عامل المقارنة:
 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){>

Autoboxing وUnboxing مع طريقة التحميل الزائد

في طريقة التحميل الزائد، يمكن إجراء الملاكمة وفك الملاكمة. هناك بعض القواعد الخاصة بطريقة التحميل الزائد في الملاكمة:
اتساع يدق الملاكمة
اتساع يدق varargs
الملاكمة تتفوق على فارارجس

1) مثال على Autoboxing حيث يتفوق التوسيع على الملاكمة

إذا كان هناك إمكانية للتوسيع والملاكمة، فإن التوسيع يتفوق على الملاكمة.
 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) مثال على Autoboxing حيث يتفوق التوسيع على varargs

إذا كان هناك احتمال للتوسيع والتباين، فإن التوسيع يتفوق على var-args.
 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) مثال على Autoboxing حيث تتغلب الملاكمة على varargs

دعونا نرى البرنامج الذي تتفوق فيه الملاكمة على الوسيطة المتغيرة:
 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 

طريقة التحميل الزائد بالتوسيع والملاكمة

لا يمكن تنفيذ عملية التوسيع والملاكمة كما هو موضح أدناه:
 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