How to Use Null Instruction in Java: 6 Steps

Table of contents:

How to Use Null Instruction in Java: 6 Steps
How to Use Null Instruction in Java: 6 Steps
Anonim

In programming, the special value NULL indicates that a variable does not refer to any specific object or value. To perform a comparison with the NULL value within your code you can use the "if" statement. The NULL value is commonly used to check if an element (object, value, method) exists or not. Used in this context, the NULL value can be used to control starting or stopping the execution of other processes or sequences of statements within code.

Steps

Part 1 of 2: Comparing an Object with the Null Value in Java

Check Null in Java Step 1
Check Null in Java Step 1

Step 1. Use the "=" operator to define a variable

The single "=" symbol is used in Java to declare a variable and assign it a certain value. You can use this operator to set a variable with the value NULL.

  • The values "0" and NULL do not represent the same entity in programming and must be managed in a different way.
  • Variable_Name = null;

Check Null in Java Step 2
Check Null in Java Step 2

Step 2. Use the "==" comparison operator to compare a variable with a specific value or with another object of the same nature

The "==" operator is used in Java to compare two values and know if they are equal or not. If, after setting the value of a variable to NULL using the "=" operator, you compare it to NULL, the program should return the boolean value "true".

  • Variable_Name == null;

  • You can also use the "! =" Comparison operator to verify that the value of a variable is NOT equal to NULL.
Check Null in Java Step 3
Check Null in Java Step 3

Step 3. Use the "if" statement to compare against the NULL value

The result obtained from the expression given in the previous step is a Boolean value ("true" or "false") that can be used as a condition of an "if" statement to tell the program what to do based on the result of the comparison..

For example, if the tested value is equal to NULL, you can print the message "The object is equal to NULL" on the screen. If the object or value tested is not equal to NULL, the statements contained in the "if" block will not be executed and the program will proceed as indicated

Object Object = null; if (Object == null) {System.out.print ("Object is equal to NULL"); }

Part 2 of 2: Uses of Null Value

Check Null in Java Step 4
Check Null in Java Step 4

Step 1. Use the special value NULL as a comparison term when you don't know the value of a certain object

In Java it is common to use NULL as the default value in place of any assigned value.

  • string ()

  • . This code indicates that the value of the string object is currently set to NULL until it is actually used.
Check Null in Java Step 5
Check Null in Java Step 5

Step 2. Use the NULL value as a condition to terminate the execution of a process

Returning the NULL value can be useful for terminating the execution of a loop of statements or for aborting a process. It is typically used most often to generate an error or raise an exception when normal program operation has stopped or when an unexpected condition has occurred.

Check Null in Java Step 6
Check Null in Java Step 6

Step 3. Use the NULL value to indicate that an object or element has not yet been initialized

Similar to the previous step, the NULL value can be used as an indicator that the execution of a process has not yet started or as a condition for the execution of a block of instructions.

For example, you can use the NULL value to control the execution of a loop of statements until a given object equals NULL or vice versa to wait until the tested element takes on a value other than NULL

synchronized method () {while (method () == null); method (). Execute_Procedure (); }

Recommended: