Every programming language developed to date contains “null”. The need for the creation of the null arose when there was a requirement to show the absence of something. While coding in Java the most usual encounter with null occurs when the “NulPointerException” is raised. Therefore, this keyword requires proper working when implemented in the code.
In this blog post, we will elaborate on the following topics in detail.
- What is Null in Java?
- How to Use the “Null” Keyword?
- Default Value of “Null” in Java
- Basic Function of Null
- Null on Static vs Non-static Method
- Implementation of Null on Autoboxing and Unboxing
- Null on instanceof Operator in Java
- Null as an Argument
- Null on Different Operators
- NullPointerException in Java
- Conclusion
Now let us move forward to understand how the null in Java works, and what are the impacts of the null keyword on different methods in Java.
What is Null in Java?
The keyword “null” in Java is a term that represents no value or nothing. Null refers to the absence of anything in the code in Java. The object declared with the “null” value has nothing associated with it. It is to keep in mind that null is not an object, nor a data type in Java it is just a keyword referring to nothing.
How to Use the Keyword “null” in Java?
Since Java is a case-sensitive programming language, therefore, the “null” keyword is also case-sensitive in nature and throws an error when not declared in the code properly. The code below shows how the null keyword is defined in Java.
class NullExample3{
public static void main(String[] args){
//Null assigned to the String
String str = Null;
System.out.println("The Output for String is: " + str);
}
}
In the above code block:
- The class is declared to implement the null in Java.
- The string object “str” is declared as “Null”.
- The println() method, prints the output for the string.
Output
The output below shows that the “Null” keyword is not defined in a proper manner since Java is a case-sensitive language.

Default Value of Null in Java
The default value for null in different scenarios while coding in Java is depicted in the code below.
public class NullExample {
private static Object nullobject;
private static int novalue;
public static void main(String[] args) {
int i = 100;
//Print the integer
System.out.println("The Output for the Integer is:" + i);
//uninitialized primitive type
System.out.println("The Output for Primitive datatype is:" + novalue);
//The output for the reference type
System.out.println("The Output for the Reference datatype is:" + nullobject);
}
}
In the above code block:
- The class is declared using the public keyword “NullExample”.
- The “int” is declared as “novalue”.
- In the main() method of Java, the integer “i” is declared a value of 100.
- The results are printed accordingly.
Output
The output below shows the default value of reference, primitive, and null in Java.
Basic Function of Null in Java
In Java, there are two basic types of data that are the primitive data type and the reference data type. The primitive data types in Java can not be declared as null whereas the reference data types can be declared as “null”. The code examples below will help to understand the above explanation.
Example 1: Null on the Reference DataType in Java
First, we will implement null on the reference data type as depicted in the code below.
class NullExample1{
public static void main(String[] args){
//Null assigned to Strings
String myStr = null;
System.out.println("The Output for String is: " + myStr);
//Null assigned to the reference object
Integer a = null;
System.out.println("The Output for Integer is: " + a);
}
}
In the above code block:
- The String is declared a null value and the output is printed.
- The integer is also declared a null value and the respective results are printed.
Output
The output below shows that “null” is returned for both the strings and the integers in Java.

Example 2: Null on the Primitive Datatype in Java
The code below shows the implementation of the primitive Datatype in Java.
class NullExample2{
public static void main(String[] args){
//Null assigned to Integer of Primitive data type
int a = null;
System.out.println("The Output for Integer is: " + a);
}
}
In the above code block:
- The primitive type “int” is assigned with a value of null.
- The output is printed.
Output
The output below shows the error as “incompatible datatypes” since primitive types in Java can not be declared as null.

Implementing Static Vs Non-Static Methods of Java
A method that belongs to the class is termed a “Static Method” and the method that belongs to the instance of the class is termed a “non-static Method” in Java. Let’s go through the following code to implement “null” on static and non-static methods in Java.
//Create a class for static and non-static methods
public class NullExample {
//main() method of Java
public static void main(String args[])
{
NullExample data = null;
data.staticMeth();
data.nonstaticMeth();
}
private static void staticMeth()
{ //It will print the output
System.out.println("This is Static Method in Java");
} //This method will not print the output
private void nonstaticMeth()
{
System.out.print("This is Non-Static Method in Java");
}
}
In the above Java code:
- The class is created as “NullExample”.
- In the next step, the object “data” for the class is declared as “null”.
- When the static method calls the object, it prints the result accordingly.
- When a non-static method calls the object declared as “null”, the output is printed in the form of an error.
Output
The output below shows that for the static method, the output is printed but for the non-static method the output is printed as “NullPointerException” since the non-static method is called through a reference object that contains a null value.

AutoBoxing and Unboxing of Null in Java
Autoboxing refers to the conversion of the primitive datatypes into its reference object. Like the conversion from “int” to “Integer”. Whereas unboxing in Java is the reverse of what autoboxing does which is the conversion of the reference object to the primitive datatype. For example conversion from “Integer” to “int”.
When implementing autoboxing and unboxing in Java there is a special need to handle “null” in a proper manner otherwise an error occurs as discussed in the code below.
public class NullExample {
public static void main(String[] args)
throws java.lang.Exception
{ //An integer can be assigned with a null value
Integer i1 = null;
System.out.println("The Output when Reference object is assigned: " + i1);
//int will throw an exception
int i2 = i1;
System.out.println("The Output when Primitive data type is assigned: " + i2);
}
}
In the above code block:
- In the main() method of Java, there are two integers declared as “i1” and “i2”.
- The first integer “i1” is assigned a “null” value and output is printed.
- In the last step, the primitive datatype int declared as “i2” is assigned to integer “i1” which is null, thus it throws the exception.
Output
The output for the reference object “Integer” is printed as “null” whereas, the primitive data type throws a “NullPointerException”.

Null on instanceof Operator in Java
The instanceof in Java tests whether the declared “object” belongs to the interface, subclass, or class in Java. It returns the output as “true” or “false”. The code below shows how instanceof is affected by “null” in Java.
public class NullExample {
public static void main(String[] args)
throws java.lang.Exception
{ //Assign the values to the integer
Integer x = null;
System.out.println("The output for 'null' is " + (x instanceof Integer));
//The value is printed
Integer y = 150;
System.out.println("The output for the Integer as '150' is " + (y instanceof Integer));
}
}
In the above Java code:
- In the main() method of Java, the first integer “x” is assigned a “null” value.
- The output is printed using the println() method with the instanceof operator.
- In the last part of the code, the second integer is assigned a value of “150” and the output is printed.
Output
The output shows that the instanceof operator returns “false” for a null value. On the other hand, it returns “true” for the integer value declared as ‘150’.

Null as an Argument in Java
In Java, null can be assigned to the argument as shown in the code below. One important thing to note is the type of the argument should be the reference type otherwise an error will occur.
import java.io.*;
//The class is declared in Java
class NullExampleArgument {
public static void null_exmp(String st)
{ //Declare the string in Java
System.out.println("My value is = " + st);}
public static void main(String[] args)
{
NullExampleArgument.null_exmp(null);
}
}
In the above code block:
- An object of string “st” is declared.
- The string is printed using the println() method of Java.
- In the last part, a null value is assigned to the method.
Output
The output below depicts that “null” can be printed as an argument in Java.

Null on the Operators in Java
A null value can be assigned to comparison, not equals to, and concatenation(+) operator in Java as shown in the code below.
public class NullOperators {
public static void main(String args[])
{ //The comparison operator
System.out.println("The Output for the Comparison'==' Operator is: " + (null == null));
//No equals to the operator
System.out.println("The Output for the NotEquals to'!=' Operator is: " + (null != null));
String s1 = null;
String s2 = ",Strings";
String result = s1 + s2;
System.out.println("The Output using the '+' Operator is: " + result);
}
}
In the above code block:
- The class is created as “NullOperators” in Java.
- The first part of the code discusses the implementation of the comparison (==) and the not equal to(!=) operator.
- In the second part of the code, two strings are declared. The first string “s1” is assigned as “null” and the other string “s2” is assigned as “,Strings”.
- The concatenated result of the two strings is printed using the ‘+’ operator by the println() method.
Output
The code below shows that the value “null== null” is true whereas using the not equals operator a false is printed for “null!=null”. The other output shows that the concatenated string along with “null” is printed.

NullPointerException in Java
This exception can occur anytime when a null value is passed and the present method does not support the null value. The code below shows that a ‘null’ is used as the delimiter in the join() method of Java to join the declared strings. This will eventually result in an error since null can not be used as a delimiter.
class NullException {
// main method
public static void main(String argvs[])
{
String s = null;
String output = String.join(s, "Java", "is", "a", "Programing Language");
System.out.println("The Output is " + output);
}
}
In the above code block:
- The string ‘s’ is declared with a “null” value.
- The join() method joins the null value which basically acts as a delimiter to join the strings.
- The output is printed accordingly.
Output
A ‘NullPointerException’ is raised in the output below since “null” can not be used as a delimiter in the join() method to join the strings in Java.

This concludes the discussion on null in Java.
Conclusion
Null is a keyword in Java that refers to the absence of something in Java. This keyword is implemented in different scenarios like it can be passed as an argument. The null value can be used in different operators like the comparison and concatenation operators in Java. Apart from that the most common encounter with the null value occurs when NullPointerException is raised in the code. In this Java post, we have compiled multiple scenarios of the null value in Java.