logo

Kako provjeriti null u Javi?

U Javi, ništavan je doslovno. Uglavnom se koristi za dodjeljivanje nulte vrijednosti varijabli. Nulta vrijednost moguća je za niz, objekt ili datum i vrijeme itd. Ne možemo dodijeliti nultu vrijednost primitivnim tipovima podataka kao što su int, float itd.

U programiranju obično trebamo provjeriti je li objekt ili niz null ili nije da izvršimo neki zadatak na njemu. Kako bismo provjerili nulti niz, imamo neke unaprijed definirane metode niza. Uzmimo neke primjere različitih tipova podataka da bismo razumjeli kako možemo provjeriti jesu li null ili ne.

Niz

U Javi, String može biti null, prazan ili prazan, a svaki od njih je različit.

python ispis na 2 decimalna mjesta

1. Prazan niz je objekt niza koji ima neku vrijednost, ali mu je duljina jednaka nuli. Na primjer:

 String str1 = '' 

2. Prazan niz je niz koji ima razmak kao vrijednost. Njegova duljina je uvijek veća od 0 i nije ni prazna ni nula. Na primjer:

 String str2 = '' 

3. Nulti niz nema vrijednost i čini niz nultim dodjeljivanjem a ništavan ključna riječ kao vrijednost za to. Na primjer:

 String str3 = null 

Kako bismo provjerili je li String null ili ne, koristimo operator usporedbe (==). Uzmimo njegov primjer da bismo razumjeli kako ga možemo koristiti za provjeru nule.

NullCheckExample1.java

 // import required classes and packages packagejavaTpoint.javacodes; // create class NullCheckExample1 class to check whether a string is empty or null class NullCheckExample1 { // main() method start public static void main(String[] args) { // create null, empty, and blank strings String string1 = null; String string2 = ''; String string3 = ' '; String string4 = 'Test'; // check whether string1 is null, empty, or blank System.out.println('string1 is ' + checkNullEmptyBlank(string1)); // check whether string2 is null, empty, or blank System.out.println('string2 is ' + checkNullEmptyBlank(string2)); // check whether string3 is null, empty, or blank System.out.println('string3 is ' + checkNullEmptyBlank(string3)); // check whether string4 is null, empty, or blank System.out.println('string4 is ' + checkNullEmptyBlank(string4)); } // create checkNullEmptyBlank() method which check whether the string is empty, null or blank and return result to the main() method public static String checkNullEmptyBlank(String strToCheck) { // check whether the given string is null or not if (strToCheck == null) { return 'NULL'; } // check whether the given string is empty or not else if(strToCheck.isEmpty()) { return 'EMPTY'; } // check whether the given string is blank or not else if(strToCheck.isBlank()) { return 'BLANK'; } else { return 'neither NULL, EMPTY nor BLANK'; } } } 

Izlaz:

Kako provjeriti null u Javi

Objekt Date andDateTime

I datum i datumvrijeme nisu primitivni tipovi podataka, pa mogu pohraniti nultu vrijednost. Uzmimo primjer objekta datuma i vremena da bismo razumjeli kako možemo provjeriti nulti objekt datuma ili datuma i vremena.

stvaranje niza nizova u Javi

NullCheckExample2.java

 //import required classes and packages packagejavaTpoint.javacodes; importjava.text.ParseException; importjava.text.SimpleDateFormat; importjava.util.Date; importjava.util.Scanner; //create class NullCheckExample2 class to check whether the Date object is null or not public class NullCheckExample2 { // declare a variable of date type and initialize it with null public static Date d1 = null; public static Date finalResult; // main() method start public static void main(String args[]) { // declare a variable of type string that will store the user-entered date in string format String d2; // create scanner class object Scanner sc = new Scanner(System.in); System.out.print('Enter Date in dd/mm/yyyy format:'); // take input from user and initialize variable d2 = sc.nextLine(); // close scanner class obj sc.close(); // create an instance of the SimpleDateFormat class for modifying the date object SimpleDateFormatobj = new SimpleDateFormat('dd/MM/yyyy'); // use try-catch to parse string to date try { finalResult = obj.parse(d2); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } // check whether date1 and date2 is null or not by using comparison operator if(d1 == null) { System.out.println('Date d1 is NULL.'); } if(finalResult == null) { System.out.println('Date d2 is NULL.'); }else { System.out.println('Date d2 is not NULL.'); } } } 

Izlaz:

Kako provjeriti null u Javi

Java objekt

Kako bismo provjerili je li Java objekt Null ili ne, možemo koristiti isNull() metodu Predmeti klasa ili operator usporedbe. Uzmimo primjer da bismo razumjeli kako možemo koristiti isNull() metodu ili operator usporedbe za null provjeru Java objekta.

NullCheckExample3.java

 // import required classes and packages packagejavaTpoint.javacodes; // create NullCheckExample3 class to check whether Java object is null or not public class NullCheckExample3 { // main() method start public static void main(String[] args) { // create instance of User2 User2 userObj = new User2(); // get data of User2 by calling User1 firstUser = userObj.getFirstUser(); // check whether firstUser is null or not if (firstUser == null) { System.out.println('Object is Null'); } else { System.out.println('Object is not Null'); // set name for user1 firstUser.setName('Paul'); System.out.println(firstUser.getName()); } } } // create User1 class having a name attribute class User1 { // declare name variable String name; // getter for getting name of User1 public String getName() { // return name of User1 return name; } // setter for setting name of User1 public void setName(String name) { this.name = name; } } // create class User2 having variable of type User1 class User2 { User1 obj; // getter to get object of User1 public User1 getFirstUser() { returnobj; } } 

Izlaz:

puni oblik pvr
Kako provjeriti null u Javi

NullCheckExample4.java

 // import required classes and packages packagejavaTpoint.javacodes; importjava.util.Objects; // create NullCheckExample3 class to check whether Java object is null or not public class NullCheckExample4 { // main() method start public static void main(String[] args) { // create instance of User2 User2 userObj = new User2(); // get data of User2 by calling User1 firstUser = userObj.getFirstUser(); // check whether firstUser is null or not if (Objects.isNull(firstUser) ){ System.out.println('Object is Null'); } else { System.out.println('Object is not Null'); // set name for user1 firstUser.setName('Paul'); System.out.println(firstUser.getName()); } } } // create User1 class having a name attribute class User1 { // declare name variable String name; // getter for getting name of User1 public String getName() { // return name of User1 return name; } // setter for setting name of User1 public void setName(String name) { this.name = name; } } // create class User2 having variable of type User1 class User2 { User1 obj; // getter to get object of User1 public User1 getFirstUser() { returnobj; } } 

Izlaz:

Kako provjeriti null u Javi