search
HomeJavaJavaBaseHow to determine whether an object is null in java

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.