Java Null Keyword
Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's use of null is somewhat infamous. Perhaps encountering the dreaded NullPointerException is a rite of passage for all Java developers. Knowing how Null functions will enable you to use it appropriately and prevent problems.
When you want to indicate the lack of a value, you can always refer to the special constant null, which is a literal in the Java language. A typical misunderstanding that novices to the Java language must contend with is that it is neither an object nor a type.
Like how there is a default value for basic types, reference types have a default value of null (such as 0 for integers and false for Booleans). Any uninitialized reference variable, including instance variables and static variables, has a default value of Null (although you will still receive a compiler warning for uninitialized local variables).To indicate the lack of anything, the concept of null was developed. In Java, you must specify the type of data that a variable is expected to hold when you define it. Variables can be thought of as containers that hold values of either of the two main kinds of categories:
- The programming language offers primitives as specified data types of the underlying value are directly present in the variable when you declare it as a primitive type (like int or float).
- References are pointers pointing to the values being represented. When a variable is specified as a reference type, the address that leads to the value rather than the actual value is stored. Reference types include things like classes, arrays, and strings.
Important points to remember about null:
- Generally null is a case sensitive one dependent on every individual alphabet.
- Null keyword is the reference variable's value.
- A NullPointerException is caused by attempting to access a null reference.
- Null is not permitted to be passed as a value when calling any primitive data type method.
- calling a variable that's not initialised
- utilising a null object to access or alter a data field or member
- supplying a method with a null object as an argument
- using a null object to call a method
- Syncing an empty object
- launching a blank object
- Using a Java array to treat a null object
Examples of Java Null keyword
Example 1:
It is a Simple illustration of showing the reference variable's default value.
public class NullEx 1 {
static NullEx1 bj;
public static void main (String [] s) {
System.out.println(bj);
}
}
Output:
Null
Example 2:
It is a case study to show how null can be passed to object reference variables.
public class NullEx2 {
public static void main (String [] s) {
NullEx2 bj = null;
System.out.println(bj);
}
}
Output:
null
Example 3:
An example to illustration of showing the default String value.
public class NullEx3 {
String raj;
public static void main (String [] s) {
NullEx3 bj=new NullEx3();
System.out.println(bj.raj);
}
}
</pre></div>
<p><strong>Output:</strong></p>
<div class="codeblock3"><pre>
null
</pre></div>
<h2 class="h3">Example 4</h2>
<p>Let's see an example to return null from a method. </p>
<div class="codeblock"><textarea name="code" class="java">
public class NullEx4 {
String disp ()
{
return null;
}
public static void main (String [] s) {
NullEx4 bj=new NullEx4();
System.out.println(obj. disp ());
}
}
Output:
Null
Example 4:
Program for illustration of how to give the String variable null.
public class NullEx5 {
static String raj=null;
public static void main (String [] s) {
if(raj==null)
{
System.out.println("its value is null");
}
else
{
System.out.println("its value is not null");
}
}
}
Output:
Its value is null
Example 5:
Reply with empty collections rather than null
It's recommended to return empty collections rather than null values.
public class empty Str 5 {
private static List<Integer> num = null;
public static List<Integer>getList () {
if (num == null)
return Collections.emptyList();
else
return num;
}
}