The Java language supports two data types, namely basic data types and reference data types, and null is a special reference data type.
To determine whether an object is null, you can use if (obj == null) { }. The code is as follows:
// 判断对象是否为null if (str1 != null) { int len = str1.length(); }
If you change the above code to the following code:
String str2 = ""; int num = str2.length(); System.out.println(num); // 输出结果为0
No exception is thrown when running the above code, because str2 is a string object with a value of "" . "" represents an empty string with a length of 0, which will be allocated a space in the memory. str2 is an instantiated object that directly points to the "" memory space.
There are two main possibilities for generating empty objects:
1. The programmer forgot to instantiate himself, so the programmer must prevent this from happening and should carefully check his own code. Instantiate and initialize all objects you create.
2. The empty object is passed from other places and needs to be avoided by determining whether the object is null.
For more java knowledge, please pay attention to the java basic tutorial column.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment