logo

حقائق عن null في جافا

كل لغة برمجة، بما في ذلك Java، محاطة بـ null. لا يوجد مبرمج لم يواجه أي مشكلة في الكود المتعلق بالـ null. يواجه المبرمجون بشكل رئيسي NullPointerException عندما يحاولون إجراء بعض العمليات باستخدام بيانات فارغة. NullPointerException هي فئة متاحة تنتمي إلى حزمة java.lang.

قبل فهم حقائق القيمة null، يجب أن تكون لديك معرفة بمتغيرات Java. إذا كنت لا تعرف ماذا متغير جافا هو الدخول على الرابط التالي :

يجب أن يكون لدى كل مطور معرفة بالحقائق التالية المتعلقة بالقيمة الفارغة في Java:

حساسية الموضوع

في Java، لا يمكننا كتابة null كـ NULL أو 0 كما هو الحال في برمجة C لأن null هو قيمة حرفية والكلمات الأساسية حساسة لحالة الأحرف في Java.

لنأخذ مثالاً لفهم سلوك null الحساس لحالة الأحرف.

NullExample1.java

 // import classes and packages if any public class NullExample1 { // main() method start public static void main (String[] args) throws java.lang.Exception { // declare and initialize an object with NULL Object obj1 = NULL; // declare and initialize an object with null Object obj2 = null; // print both objects System.out.println('The value of obj1 is: '+obj1); System.out.println('The value of obj2 is: '+obj2); } } 

انتاج:

حقائق عن null في جافا

القيمة المتغيرة المرجعية

بشكل افتراضي، كل متغير مرجعي له قيمة فارغة في Java. يتم استخدام متغير مرجعي للإشارة إلى الكائنات/القيم ذات النوع المرجعي وتخزينها في Java. تعد الفئات والمصفوفات والتعدادات والواجهات وما إلى ذلك بعض أنواع المراجع في Java.

إضافة إلى مجموعة جافا

لذلك، يقوم نوع المرجع بتخزين قيمة فارغة إذا لم يتم تمرير أي كائن إلى نوع مرجع.

لنأخذ مثالاً لفهم كيفية عمل المتغير المرجعي للقيمة الخالية:

NullExample2.java

 // import classes and packages if any public class NullExample2 { // declare two objects private static Object obj1; private static Object obj2; // main() method start public static void main (String[] args) { // print both objects System.out.println('The value of obj1 is: '+obj1); System.out.println('The value of obj2 is: '+obj2); } } 

انتاج:

حقائق عن null في جافا

نوع فارغ

في Java، null ليس كائنًا ولا نوعًا. إنها قيمة خاصة يمكننا تخصيصها لأي متغير نوع مرجعي. يمكننا تحويل القيمة null إلى أي نوع نريده، مثل string، int، double، إلخ.

لنأخذ مثالاً لفهم كيف يمكننا تعيين قيم فارغة لأي نوع مرجعي.

NullExample3.java

 // import classes and packages if any public class NullExample3 { // main() method start public static void main (String[] args) { // pass a null value to a different type of variable // pass null to String String str = null; // pass null to Integer Integer itr = null; // pass null to Double Double dbl = null; // casting null to String String castedStr = (String)null; // casting null to Integer Integer castedItr = (Integer)null; // casting null to Double Double castedDbl = (Double)null; // print all reference type System.out.println('The value of str is: '+str); System.out.println('The value of itr is: '+itr); System.out.println('The value of dbl is: '+dbl); System.out.println('The value of castedStr is: '+castedStr); System.out.println('The value of castedItr is: '+castedItr); System.out.println('The value of castedDbl is: '+castedDbl); } } 

انتاج:

حقائق عن null في جافا

Autoboxing وUnboxing

التعبئة التلقائية و الإخراج من العلبة هما أهم العمليات التي نقوم بها في جافا. يلقي المترجم NullPointerException عندما نقوم بتعيين قيمة فارغة لأي نوع بيانات محاصر بدائي أثناء إجراء العمليات.

لنأخذ مثالاً لفهم عملية التحميل التلقائي وحقيقة إلغاء العلبة الفارغة.

NullExample4.java

 // import classes and packages if any public class NullExample4 { // main() method start public static void main (String[] args) throws java.lang.Exception { // pass null value to a reference type Integer itr = null; // perform unboxing operation int data = itr; // print both objects System.out.println('The value of itr is: '+itr); System.out.println('The value of data is: '+data); } } 

انتاج:

حقائق عن null في جافا

مثيل المشغل

من أجل التحقق مما إذا كان هدف هل هو مثيل من النوع المحدد أم لا، نستخدم حالة المشغل أو العامل. ال حالة يُرجع عامل التشغيل صحيحًا عندما لا تكون قيمة التعبير فارغة في وقت التشغيل. إنه يلعب دورًا مهمًا في عمليات التحقق من الكتابة.

لنأخذ مثالا لفهم حالة المشغل أو العامل:

NullExample5.java

 // import classes and packages if any public class { // main() method start public static void main (String[] args) throws java.lang.Exception { // pass null value to a reference type Integer m = null; // pass a value to a reference type Integer n = 20; // print instanceof values System.out.println(m instanceof Integer); System.out.println(n instanceof Integer); } } 

انتاج:

حقائق عن null في جافا

ثابت مقابل. طرق غير ثابتة

لا يمكننا استدعاء طريقة غير ثابتة على متغير مرجعي بقيمة فارغة. إذا استدعيناها، فسوف ترمي NullPointerException، لكن يمكننا استدعاء الطريقة الثابتة بمتغيرات مرجعية ذات قيم فارغة. نظرًا لأن الطرق الثابتة مرتبطة باستخدام الربط الثابت، فلن تؤدي إلى استثناء مؤشر فارغ.

لنأخذ مثالاً لفهم حقيقة الصفر:

الشبكات وأنواعها

NullExample6.java

 // import classes and packages if any public class NullExample6 { // define static method public static void staticMethod() { //it can be called by using a null reference System.out.println('static method can be called by null reference.'); } // define non static method public void nonStaticMethod() { //it cannot be called by using a null reference System.out.println('static method cannot be called by a null reference.'); } // main() method start public static void main (String[] args) throws java.lang.Exception { NullExample6 exp = null; exp.staticMethod(); exp.nonStaticMethod(); } } 

انتاج:

حقائق عن null في جافا

== و!= عوامل التشغيل

في Java، يُسمح بهذين العاملين باستخدام القيمة null. يعد كلا العاملين مفيدًا في التحقق من القيمة الخالية مع الكائنات في Java.

لنأخذ مثالاً لفهم كيفية عمل هذين العاملين مع القيمة الخالية.

NullExample7.java

 // import classes and packages if any public class NullExample7 { // main() method start public static void main (String[] args) { // pass null value to String type of variables // pass null to str1 String str1 = null; // pass null to str2 String str2 = null; // pass null to str3 String str3 = 'Test'; // compare strings if(str1 == str2){ System.out.println('str1 and str2 both are equal'); }else{ System.out.println('str1 and str2 are not equal'); } if(str2 == str3){ System.out.println('str2 and str3 both are equal'); }else{ System.out.println('str2 and str3 are not equal'); } if(str3 == str1){ System.out.println('str3 and str1 both are equal'); }else{ System.out.println('str3 and str1 are not equal'); } } } 

انتاج:

حقائق عن null في جافا

يجب أن يكون لدى كل مطور Java معرفة بجميع الحقائق التي تمت مناقشتها أعلاه حول القيمة الفارغة.