Home>Article>Java> How to determine whether a string is empty or null in java?

How to determine whether a string is empty or null in java?

青灯夜游
青灯夜游 Original
2019-12-31 16:12:10 4621browse

How to determine whether a string is empty or null in java?

#How does Java determine whether a string is empty or null?

First, distinguish between empty strings and null strings:

The empty string "" is a string of length 0, which has its own string length (0) and content (empty), the method to determine whether a string is empty:

if (str.length() == 0)

or

if (str.equals(""))

null string means that there is currently no object associated with the variable, the method to check whether a string is null:

if (str == null)

Check that a string is neither a null string nor an empty string. Use the following method to determine:

if (str != null && str.length() != 0)

Note: You must first check that str is not null, otherwise in An error will occur if the length() method is increased by a null value.

Use the StringUtils tool class to determine whether it is neither null nor empty, as follows:

if (StringUtils.isNotBlank(str))

Recommended learning:Java video tutorial

The above is the detailed content of How to determine whether a string is empty or 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