Is null a literal in Java?


The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc. but can be cast to a reference type. Also, null does not have the value 0 necessarily.

A program that demonstrates null in Java is given as follows:

Example

 Live Demo

public class Demo {
   public static void main(String args[]) {
      String str = null;
      System.out.println("Value of str is: " + str);
   }
}

Output

Value of str is: null

Now let us understand the above program.

In the main() method, the String str is initialized as null. Then the value of str is printed. A code snippet which demonstrates this is as follows:

String str = null;
System.out.println("Value of str is: " + str);

Updated on: 30-Jul-2019

617 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements