Java null - The Coding Shala

Home >> Learn Java >> Java NULL

Java null

Java null is present in java.lang package. The following are some properties of null-
  • null is case sensitive that means NULL and null are different. In Java, we use null(small). NULL will give an error.
  • Java null is a special value, it does not object or neither a type. It can be assigned to any reference type or can typecast null to any type.
Point to remember: By default, any reference variable in Java has a null value.
Java null - The Coding Shala
  • Non-static methods cannot be called on a reference variable with a null value.
  • In java, we can use equals(==) and not equals(!=) operators with null.
public class NULL{
    private static Object obj;
    public static void main(String[] args){
        NULL Null_obj = null;
        Null_obj.Static_Method();
     //   Null_obj.NonStatic(); //will give  java.lang.NullPointerException error
        //Object name = null;  //like this
        //Object name = NULL;  //will give error;
        Integer it = null;
        System.out.println(it);
        System.out.println(obj);
        String str = (String) null;
        System.out.println("This is String "+str);
        if(null==null){
            System.out.println("In null equals");
        }
        if(it!=null){
            System.out.println("it is not null");
        }else{
            System.out.println("it is null");
        }
    }
    private static void Static_Method(){
        System.out.println("This is static method call by null reference value");
    }
    private void NonStatic(){
        System.out.println("Non static method called by null reference value");
    }
}


-------output--------
This is static method call by null reference value
null
null
This is String null
In null equals
it is null



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala