136

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 ?

Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140

8 Answers8

229

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)

Matt
  • 23,363
  • 39
  • 111
  • 152
Alex
  • 25,147
  • 6
  • 59
  • 55
  • @sharonHwk "person == null" should be the better option. If person is null, it won't have a "equal" method, then "person.equals(null)" throws. – CodingFanSteve Mar 09 '19 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 '19 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 07 '20 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.

user207421
  • 305,947
  • 44
  • 307
  • 483
PermGenError
  • 45,977
  • 8
  • 87
  • 106
21

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

Zutty
  • 5,357
  • 26
  • 31
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){}.

Community
  • 1
  • 1
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
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.

MacGregor
  • 51
  • 4
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...

melli-182
  • 1,216
  • 3
  • 16
  • 29
  • @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 06 '12 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
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55