How to check if an int is a null

JavaInt

Java Problem Overview


I have an object called Person.

it has several attributes in it;

int id;
String name;

i set a person object like Person p = new Person(1,"Joe");.

1.) I need to check if the object is not null; Is the following expression correct;

if (person == null){
}

Or


if(person.equals(null))

2.) I need to know if the ID contains an Int.

if(person.getId()==null){} 

But, java doesn't allow it. How can i do this check ?

Java Solutions


Solution 1 - Java

An int is not null, it may be 0 if not initialized.

If you want an integer to be able to be null, you need to use Integer instead of int.

Integer id;
String name;

public Integer getId() { return id; }

Besides the statement if(person.equals(null)) can't be true, because if person is null, then a NullPointerException will be thrown. So the correct expression is if (person == null)

Solution 2 - Java

primitives dont have null value. default have for an int is 0.

if(person.getId()==0){}

Default values for primitives in java:

Data Type	Default Value (for fields)

byte            	0
short           	0
int	                0
long	        0L
float	        0.0f
double	        0.0d
char	        '\u0000'
boolean	        false

Objects have null as default value.

String (or any object)--->null

> 1.) I need to check if the object is not null; Is the following expression correct;

if (person == null){
}

the above piece of code checks if person is null. you need to do

if (person != null){ // checks if person is not null
}

and

if(person.equals(null))

The above code would throw NullPointerException when person is null.

Solution 3 - Java

A primitive int cannot be null. If you need null, use Integer instead.

Solution 4 - Java

> 1.) I need to check if the object is not null; Is the following expression correct; > > if (person == null){ > }

YES. This is how you check if object is null.

> 2.) I need to know if the ID contains an Int. > > if(person.getId()==null){}

NO Since id is defined as primitive int, it will be default initialized with 0 and it will never be null. There is no need to check primitive types, if they are null. They will never be null. If you want, you can compare against the default value 0 as if(person.getId()==0){}.

Solution 5 - Java

You have to access to your class atributes.

To access to it atributes, you have to do:

person.id
person.name

where

> person

is an instance of your class Person.

This can be done if the attibutes can be accessed, if not, you must use setters and getters...

Solution 6 - Java

In Java there isn't Null values for primitive Data types. If you need to check Null use Integer Class instead of primitive type. You don't need to worry about data type difference. Java converts int primitive type data to Integer. When concerning about the memory Integer takes more memory than int. But the difference of memory allocation, nothing to be considered.

In this case you must use Inter instead of int

Try below snippet and see example for more info,

Integer id;
String name;

//Refer this example
    Integer val = 0;

`

if (val != null){
System.out.println("value is not null");
}

`

Also you can assign Null as below,

val = null;




Solution 7 - Java

You can use

if (person == null || String.valueOf(person.getId() == null)) 

in addition to regular approach

person.getId() == 0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSharon WatinsanView Question on Stackoverflow
Solution 1 - JavaAlexView Answer on Stackoverflow
Solution 2 - JavaPermGenErrorView Answer on Stackoverflow
Solution 3 - JavaZuttyView Answer on Stackoverflow
Solution 4 - JavaYogendra SinghView Answer on Stackoverflow
Solution 5 - Javamelli-182View Answer on Stackoverflow
Solution 6 - JavaChameera W. AshanView Answer on Stackoverflow
Solution 7 - JavaevilcoderView Answer on Stackoverflow