Jak sprawdzić, czy int jest null

Mam obiekt o nazwie Person.

Ma w sobie kilka atrybutów;

int id;
String name;

Ustawiam obiekt person jak Person p = new Person(1,"Joe");.

1.) Muszę sprawdzić czy obiekt nie jest null; czy poniższe wyrażenie jest poprawne;

if (person == null){
}

Or


if(person.equals(null))

2.) Muszę wiedzieć, czy ID zawiera Int.

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

Ale java na to nie pozwala. Jak mogę to sprawdzić ?

 78
Author: sharon Hwk, 2012-12-06

7 answers

An int nie jest null, może być 0 jeśli nie jest zainicjalizowana.

Jeśli chcesz, aby liczba całkowita była null, musisz użyć Integer zamiast int.

Integer id;
String name;

public Integer getId() { return id; }

Poza tym twierdzenie if(person.equals(null)) nie może być prawdziwe, ponieważ Jeśli person jest null, to NullPointerException zostanie wyrzucone. Więc poprawnym wyrażeniem jest if (person == null)

 133
Author: Alex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-06 16:18:31

Prymitywy nie mają wartości null. domyślnie have dla int to 0.

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

Domyślne wartości dla prymitywów w Javie:

Data Type   Default Value (for fields)

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

Obiekty mają wartość null jako wartość domyślną.

String (lub dowolny obiekt) - - - > null

1.) Muszę sprawdzić czy obiekt nie jest null; czy poniższe wyrażenie jest poprawne;

if (person == null){
}

Powyższy fragment kodu sprawdza, czy osoba jest null. musisz zrobić

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

I

if(person.equals(null))

Powyższy kod rzuciłby NullPointerException gdy osoba jest null.

 37
Author: PermGenError,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-07 00:58:01

A primitive int cannot be null. Jeśli potrzebujesz null, użyj Integer.

 10
Author: Zutty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-06 16:18:23

1.) Muszę sprawdzić czy obiekt nie jest null; czy poniższe wyrażenie jest poprawne;

If (person = = null){ }

Tak . W ten sposób sprawdzasz, czy obiekt jest null.

2.) Muszę wiedzieć, czy ID zawiera Int.

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

Nie ponieważ {[1] } jest definiowane jako prymitywne int, będzie domyślnie inicjowane przez 0 i nigdy nie będzie to null. Nie ma potrzeby sprawdzania prymitywne typy, jeśli są null. Nigdy nie będą zerowe. Jeśli chcesz, możesz porównać z wartością domyślną 0 jako if(person.getId()==0){}.

 2
Author: Yogendra Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-06 16:26:09

Musisz mieć dostęp do swojej klasy atrybuty.

Aby uzyskać do niego dostęp, musisz zrobić:

person.id
person.name

Gdzie

Osoba

Jest instancją twojej klasy.

Można to zrobić, jeśli można uzyskać dostęp do attibutów, jeśli nie, należy użyć setterów i getterów...

 0
Author: melli-182,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-06 16:19:38

Możesz wykonać kontrolę null w ten sposób

 int id;
    if(person.getId()==0){
    //TO DO
    }

Lub

Integer id2;
if(person.getId2()==null){
//TO DO
}

Lub

Long id3;
if()[
//TO DO
}
 -1
Author: Oguzhan Cevik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-14 13:10:41

Możesz użyć

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

Oprócz regularnego podejścia

person.getId() == 0
 -2
Author: evilcoder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-14 17:36:43