Check if Int Is Null in Java

Haider Ali Nov 29, 2021 Nov 12, 2021
Check if Int Is Null in Java

In this guide, we will learn how to check if int is null in java. In order to understand this concept, we need to go through some basic understanding of datatype int. Let’s dive in.

Can int Be Null in Java?

One thing that we need to understand first is that int is a primitive data type. Such data types stores data in binary form in memory by default. That means they can’t be null. We certainly can’t check int for a null value. On the other hand, we can’t confuse it with the Integer which is an object and which can have a null value. An Integer is a wrapper class of int that allows the developers to have more functionalities associated with int. This is the difference that you need to understand, learn more about int and Integerhere.

public class Main
{   
    public static void main(String[] args)   
    {
        int id=0;  // Primitve DataTypes..
        Integer ID = new Integer(5);
        System.out.println( "Primitive integer : "+  id); 
        // we cannot check for Null Property 
        System.out.println( "Integer Object : "+  ID);  
        // We can check for Null Property..
         
        if(ID==null)
         {
                System.out.println("Integer Is  Null");
         }
         else
         {
            System.out.println("Integer Is  Not Null");
         }
         
    }   
} 

Output:

Primitive integer : 0
Integer Object : 5
Integer Is  Not Null

As seen in the above example, int cannot be null. On the other hand, Integer is an object which can be checked for the null property.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java Int