Home >Java >JavaBase >How to determine whether an object is null in java

How to determine whether an object is null in java

(*-*)浩
(*-*)浩Original
2019-11-15 10:55:346116browse

How to determine whether an object is null in java

Determining whether a Java object is null can have two meanings: (Recommended learning: java course)

First level: Directly use object == null to judge. When the object is null, it returns true, and when it is not null, it returns false.

Second level: When object != null is true, further determine whether all attributes of the object are null.

Simple judgment:

//判断1  : 这里会返回 true
User user = null;
if(user == null){
  return true;  
}else{
  return false;  
}

//判断2 : 这里会返回 false
User user = new User();
if(user == null){
  return true;  
}else{
  return false;  
}

原因: User user = new User(); 这时候已经创建了一个对象,所以user不会为null

The above example is relatively simple and clear at a glance, and there does not seem to be any problem.

" But in fact, when the object you need to judge is an object passed from the front end, simply using object == null to judge is not enough, because the object passed by the front end will look like the judge. The object does not is null, but all properties of the object are null. [Except boolean types, and serialized values, and there may be other special values] "

The above is the detailed content of How to determine whether an object is null in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more