Is null a literal in Java?

Java 8Object Oriented ProgrammingProgramming

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);
raja
Published on 19-Feb-2019 06:42:53
Advertisements