Home » Java programming language

What is null in Java?

null in Java: Here, we are going to learn how to work with null in Java? What operations can we perform with null in Java?
Submitted by Preeti Jain, on July 24, 2019

  • As we know null is an important concept in every language not only in Java but here we will study various factors regarding null.
  • null is a very critical factor that means we need to focus when we work with null.
  • null is a keyword in Java and it is related to NullPointerException and NullPointerException is a package in java.lang package like this java.lang.NullPointerException.
  • We will see NullPointerException throw if we perform operations with or without null in Java.

In a general way we will discuss a few cases and the cases are given below...

Case 1: We know that null is cases sensitive

Here, we will see why null is case sensitive in Java and null is a keyword in Java that's why null is case sensitive because all the keywords in java are case sensitive.

Note:

Case sensitive means the word written in small letters and Capital letters has different meanings for example: null, NULL(Both are different).

In java (null) is valid but if we write (NULL, 0, Null), etc these word is invalid and there is no sense).

Example:

class NullCaseSensitive{
public static void main(String[] args) throws Exception{
			
/*We are assigning null in str1 and it will execute without any error*/
String str1 = null;
System.out.println("The value of str1 is "+str1);
 
/*  We are assigning Null in str2 and NULL in str3 
    and it will give compile time error  because 
    Null and NULL is invalid in java
*/

/*
String str2 = Null;
System.out.println("The value of str2 is "+str2);

String str3 = NULL;
System.out.println("The value of str3 is "+str3);
*/
}
} 

Output

E:\Programs>javac NullCaseSensitive.java

E:\Programs>java NullCaseSensitive
The value of str1 is null

Case 2: We know that Reference variable hold null by default

  • In Java, the Integer reference variable holds null value by default at the time of object instantiation or in other words if we don't assign any value from our end at the time of object instantiation.
  • In Java, String reference hold null by default at the time of object instantiation if we don't assign any other value at the time of object instantiation from our end.
  • In Java, Object reference hold null by default at the time of object instantiation if we don't assign any other value at the time of object instantiation from our end.

Example:

class ReferenceVariable {
 // Declaring Reference Variable
 String str;
 Object obj;
 Integer in ;
}

class Main {
 public static void main(String[] args) throws Exception {
  ReferenceVariable rv = new ReferenceVariable();

  System.out.println("The default value of the Object reference is " + rv.obj);
  System.out.println("The default value of the String reference is " + rv.str);
  System.out.println("The default value of the Integer reference is " + rv.in);
 }
}

Output

The default value of the Object reference is null
The default value of the String reference is null
The default value of the Integer reference is null

Case 3: If we assign null to primitive data type then we will get a compile-time error

Example:

class AssignNullToPrimitive {
 public static void main(String[] args) {
  char ch = null;
  int i = null;

  /* We will get error here because we 
  can'’'t null to primitive datatype*/
  System.out.println("The value of the char is " + ch);
  System.out.println("The value of the int is " + i);
 }
}

Output

E:\Programs>javac AssignNullToPrimitive.java
AssignNullToPrimitive.java:5: error: incompatible types
char ch = null;
        ^
  required: char
  found:    <null>
AssignNullToPrimitive.java:6: error: incompatible types
int i = null;
        ^
  required: int
  found:    <null>
2 errors

Case 4: If we check whether an object is an instance of a class, interface, etc. it returns true if an object is not an instance of null (i.e the value of the expression is not null)else return false

Example:

class CheckObjectInstanceOf {
	public static void main(String[] args) throws Exception {
		String str = null;
		Double d = null;
		Float f = 10.0f;
		
		System.out.println("Is str is an instanceof String " + (str instanceof String));
		System.out.println("Is f is an instanceof Float " + (f instanceof Float));
		System.out.println("Is d is an instanceof Double " + (d instanceof Double));
	}
}

Output

Is str is an instanceof String false
Is f is an instanceof Float true
Is d is an instanceof Double false


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.