PDF download Download Article PDF download Download Article

A null indicates that a variable doesn't point to any object and holds no value. You can use a basic ‘if’ statement to check a null in a piece of code. Null is commonly used to denote or verify the non-existence of something. Within that context, it can be used as a condition to start or stop other processes within the code.[1]

Part 1
Part 1 of 2:

Using an If Statement

PDF download Download Article
  1. A single “=” is used to declare a variable and assign a value to it. You can use this to set a variable to null.
    • A value of “0” and null are not the same and will behave differently.
    • variableName = null;
  2. A “==” is used to check that the two values on either side are equal. If you set a variable to null with “=” then checking that the variable is equal to null would return true.
    • variableName == null;
    • You can also use “!=” to check that a value is NOT equal.
    Advertisement
  3. The result of the expression will be a boolean (true or false) value. You can use the boolean value as a condition for what the statement does next.
    • For example, if the value is null, then print text “object is null”. If “==” does not find the variable to be null, then it will skip the condition or can take a different path.
      Object object = null ;
      if ( object == null ) {
      System.out.print ( "object is null ");
      }
      
  4. Advertisement
Part 2
Part 2 of 2:

Using a Null Check

PDF download Download Article
  1. It is common to use null as a default in lieu of any assigned value.
    • string() means the value is null until it is actually used.
  2. Returning a null value can be used to trigger the end of a loop or break a process. This is more commonly used to throw an error or exception when something has gone wrong or an undesired condition has been hit.
  3. Similarly, null can be used as flag to show that a process has not yet started or as a condition to mark to be beginning of a process.
    • For example: do something while object is null or do nothing until an object is NOT null.
      synchronized method()
      { 
          while (method()==null);
          method().nowCanDoStuff();
      }
      
  4. Advertisement

Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

  • Some consider heavy use of nulls a poor practice in object oriented programming, where values should always be pointed to objects.[2] [3]
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Close a Window in JavaClose a Window in Java
Compile & Run Java Program Using Command PromptA Beginner’s Guide to Compiling & Running Java Programs
Check Your Java Version in the Windows Command LineUse Easy Windows CMD Commands to Check Your Java Version
Do Division in Java Do Division in Java (Integer and Floating Point)
Set Java Home Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
Compile and Run Java Program by Notepad Compile and Run Java Programs Using Notepad++
Check Java Version on a Mac Quickly and Easily Check Java Version on a Mac
Create an Executable File from EclipseCreate an Executable File from Eclipse
Add JARs to Project Build Paths in Eclipse (Java)Add JARs to Project Build Paths in Eclipse (Java)
Update JavaUpdate Java
Calculate Percentage in JavaCalculate Percentage in Java
Program in JavaProgram in Java
Call a Method in JavaCall a Method in Java
Create JAR File Create a JAR File With Eclipse
Advertisement

About This Article

wikiHow Staff
Co-authored by:
wikiHow Staff Writer
This article was co-authored by wikiHow Staff. Our trained team of editors and researchers validate articles for accuracy and comprehensiveness. wikiHow's Content Management Team carefully monitors the work from our editorial staff to ensure that each article is backed by trusted research and meets our high quality standards. This article has been viewed 353,631 times.
How helpful is this?
Co-authors: 13
Updated: March 21, 2023
Views: 353,631
Categories: Java
Thanks to all authors for creating a page that has been read 353,631 times.

Is this article up to date?

Advertisement