Java Null – 7 Unknown Facts about Null in Java 2019

We closed our last chapter on Type Conversion in Java. Today, we will in this Java tutorial, we are going to learn what is Java Null and seven unknown facts about the null in Java Programming Language: case sensitivity, refer variable value, types of null, autoboxing & unboxing in java, instanceof operator, static vs non- static methods, and ==and!=are some interesting facts about Null.

Java Null - 7 Unknown Facts about Null in Java 2018

Java Null – 7 Unknown Facts about Null

1. What is Java Null?

All the programming dialects are fortified with null. In Java, invalid is related java.lang.NullPointerException. As it is a class in java.lang bundle, it is called when we endeavour to play out a few activities with or without invalid and at times we don’t know where it has happened.

Read about Java Abstract Data Type in Data Structure 

2. Unknown Facts About Null in Java 2019

All the programmers on the earth are suffering with null. These are some facts, which you should know about null, to program in Java.

  • Case sensitive
  • Reference variable value
  • Type of null
  • Autoboxing and unboxing
  • instanceof operator
  • Statics vs non-static methods
  • == and !=

i. Java Null is Case Sensitive

It is a literal and in Java keywords are case sensitive, so we do not write it as NULL or 0 as we would in C.

Example –

public class Test
{
    public static void main (String[] args) throws java.lang.Exception
     {
// compile-time error : can't find symbol 'NULL'
        Object obj = NULL;       
        //runs successfully
        Object obj1 = null;
    }
}

Output –
5: error: cannot find symbol
can’t find symbol ‘NULL’
^
variable NULL
class Test
1 error

Do you know How to Handle Java Exception

ii. Reference Variable Value

Any reference variable automatically has a null value.

Example –

public class Test
{
    private static Object obj;
    public static void main(String args[])
    {
// it will print null;
        System.out.println("Value of object obj is : " + obj);
    }
}

iii. Type of Null in Java

Java Null is just a special value, it is neither an object nor a type.

Example –
String str = null;
Integer itr = null;
Double dbl = null;
String myStr = (String) null;
Integer myItr = (Integer) null;
Double myDbl = (Double) null;

Join DataFlair on Telegram

Read about Java Number Methods with Syntax and Examples

iv. Autoboxing and Unboxing in Java

An error is thrown if the null value is assigned to a primitive data type in Java.
Example –

public class Test
{
    public static void main (String[] args) throws java.lang.Exception
    {
          
            Integer i = null;
            int a = i;
    }
}

v. Instanceof Operator in Java

At runtime the instanceof operator in Java is true if an expression is Java null, it checks for the type of instance of the object.

Example –

public class Test
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Integer i = null;
        Integer j = 10;            
//prints false
        System.out.println(i instanceof Integer)
//Compiles successfully
        System.out.println(j instanceof Integer);
    }
}

vi. Static vs. Non-static Methods in Java

A non-static value cannot be called by a null value, it will throw a NullPointerException, but this won’t happen with a static method.

Read more about Methods in Java 

Example-

public class Test
 {            
     public static void main(String args[])
        {
           Test obj= null;
           obj.staticMethod();
           obj.nonStaticMethod();                            
        }    
private static void staticMethod()
        {
           System.out.println("static method, can be called by null reference");
        }        
private void nonStaticMethod()
       {      
            System.out.print(" Non-static method- ");
            System.out.println("cannot be called by null reference");
       }
  }

vii. == and != Operator

These comparisons are possible with a null operator.

Example –

public class Test
{            
    public static void main(String args[])
       {
          System.out.println(null==null);
          System.out.println(null!=null)
       }
}

3. Conclusion

Hence, we learned about the null in Java and some interesting points about it: Case Sensitive, Reference Variable Value, Type of Null, Autoboxing and Unboxing in Java, Instanceof operator in Java, Static vs. Non-static Methods in Java, == and != Operator. Furthermore, if you have any query feel free to ask in a comment section.

Related Topic- The Island of Isolation in Java with Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.