Check If Int is Null In Java

In this tutorial we will see how to check if int is null in java . First we have to know int is a primitive data type and Integer is a wrapper class in Java.

Check If Int Is Null In Java

Int is primitive data type in java and Such data types in java stores data in binary form in memory by default. That means they can’t be null in java .So we can’t check int for a null value . On the other hand, Integer is an object and it can have a null value in java . In java ,variables of the Integer type, store references to Integer objects . An Integer is a wrapper class just like other class . There are more functionalities associated with Integer class in java .

Now we will see example for check if int is null in java . In this example we have create variables of int and Integer type , then check for null in java for these variable .

public class JavaIntegerNull {
	
	public static void main(String[] args) {
		
		  // Integer primtive data type     
	      int userId = 20;
	      //create object of  Integer wrapper class
	      Integer userId2 = new Integer(80);
	      System.out.println("Primitive int userId is : "+ userId);
	      System.out.println("Wrapper class Integer object userId2  is : "+ userId2);
	     
	      /*We can not check for null property for int, We can check for 
	      null property for Integer */
	      
	      if (userId2 == null) {
	        System.out.println("Integer wrapper class object userId2  is NULL");
	      }
	      else {
	        System.out.println("Integer wrapper class object userId2 is NOT NULL");
	      }
	      		
	}

}

Output:

Primitive int userId is : 20
Wrapper class Integer object userId2  is : 80
Integer wrapper class object userId2 is NOT NULL

Now it is shown in example, that Integer is an object that can be checked for null property whereas int can not be null. Let’s see more example for java integer null checking .

Example 1 :  Java Integer null Check Example

In this example , We will create Integer object with zero(0) value , thence will check java integer null or not in java . Because object initialize with 0 value so it will not be null .

public class JavaIntegerNull1 {
	
	public static void main(String[] args) {
		 Integer userId = new Integer(0);
		
		if (userId == null) {
			System.out.println("Integer wrapper class object is NULL");
		} else {
			System.out.println("Integer wrapper class object is NOT NULL");
		}
		
		System.out.println("Integer wrapper class object : "+ userId);
		
	}

}

Output:

Integer wrapper class object is NOT NULL
Integer wrapper class object : 0

Example 2:  Java Integer null Check Example

In this example , We will initialize Integer object with null value . When well check java integer null or not ,then it shown us that Integer object is null .

public class JavaIntegerNull2 {
	
	public static void main(String[] args) {
		
         Integer userId = null;
		
		if (userId == null) {
			System.out.println("Integer wrapper class object is NULL");
		} else {
			System.out.println("Integer wrapper class object is NOT NULL");
		}
		
		System.out.println("Integer wrapper class object : "+ userId);
	      
	}

}

Output :

Integer wrapper class object is NULL
Integer wrapper class object : null

In this example we learned how to check java integer null or not . We have also got answer for can an int be null in java . you can learn more java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

77 + = 80