139

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 ?

4
  • 11
    Not only Java. A primitive type cannot be null anywhere.
    – Rohit Jain
    Dec 6, 2012 at 16:18
  • 2
    yeah, the default value of an int is 0 ? Dec 6, 2012 at 16:19
  • 2
    @sharon. Yes, it's 0. So, you can do the check with 0 instead. If you want to do the null check. Use Integer wrapper for that.
    – Rohit Jain
    Dec 6, 2012 at 16:20
  • 3
    It's not always a good idea to use an int to represent an identification code since you aren't using it as a true number -- you do no addition nor subtraction or other math operations with it. Consider using a String instead. Dec 6, 2012 at 16:21

8 Answers 8

231

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)

3
  • @sharonHwk "person == null" should be the better option. If person is null, it won't have a "equal" method, then "person.equals(null)" throws. Mar 9, 2019 at 19:09
  • 1
    "An int is not null, it may be 0 if not initialized." that's not 100% right, an int as local variable will not be initialized.
    – Emeric
    May 22, 2019 at 13:09
  • 1
    Of course, for local variable cannot be uninitialized, it's invalid code, Java won't permit it, as declared in its specification.
    – Alex
    Mar 7, 2020 at 21:43
53

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.

0
21

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

3

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){}.

1

Compiler won't let you assing value of int to null. It won't run. So we could say it is already partly solved for you.

0

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...

1
  • @RohitJain i am not saying that, im saying that is not correct to ask for person==nul if he wants to know if the id is "null". Due to the fact that id is an int, he cant ask in the way you are suggesting...
    – melli-182
    Dec 6, 2012 at 16:28
0

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;
-1

You can use

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

in addition to regular approach

person.getId() == 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.