Pages

Thursday, December 16, 2021

How To Check If int is null in Java

1. Overview

In this tutorial, We'll learn how to check if primitive int is null or not in java.

First, let us write the simple example program and see what is the output if we check int is not null using != operator.

How To Check If int is null in Java



2. Java Check If int is null or not


Create a simple class Book with the int id and String title.

Next, create an object to the Book class and check book.getId() == null using == opeartor.

Example 1
package com.javaprogramto.datatypes;

public class CheckIntIsNullExample {

	public static void main(String[] args) {

		Book book = new Book();

		if (book.getId() == null) {
			System.out.println("given book id null");
		}
	}

}

class Book {
	int id;
	String name;

	public Book() {

	}

	public Book(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The operator == is undefined for the argument type(s) int, null

	at com.javaprogramto.datatypes.CheckIntIsNullExample.main(CheckIntIsNullExample.java:9)


From the output, we could see the compile time error with the reason "The operator == is undefined for the argument type(s) int, null"

3. Is it possible to use null check on int type?


As you know int is the primitive data type and it is not considered as an object. int type default value is 0 if no value is assigned to it.

Only wrapper class instances are equivalent to the primitive types.

If you want to check int is null or not then you need to replace int type with its wrapper class Integer.

Integer classes can be used to check the value is null or not because the Integer class default value is null.

Integer class is added with the new methods in java 8 Integer api.

The below code works without any errors.

Example 2
package com.javaprogramto.datatypes;

public class CheckIntIsNullExample2 {

	public static void main(String[] args) {

		BookInteger book = new BookInteger();

		if (book.getId() == null) {
			System.out.println("given book id null");
		} else {
			System.out.println("book is given is "+book.getId());
		}
	}

}

class BookInteger {
	Integer id;
	String name;

	public BookInteger() {

	}

	public BookInteger(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

Output
given book id null

If the Integer id value is null and if you not handing null condition properly then you may get the NullPointerException.


4. Conclusion


In this article, we've seen how int can be used to check value is null or not in java.

Directly int value can not be checked as null and have to use the wrapper Integer class to check int value is null.





 


No comments:

Post a Comment

Please do not add any spam links in the comments section.