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 ?
An
intis not null, it may be0if not initialized.If you want an integer to be able to be null, you need to use
Integerinstead ofint.Besides, the statement
if(person.equals(null))can’t be true because ifpersonis null, then aNullPointerExceptionwill be thrown. So the correct expression isif (person == null)