牛骨文教育服务平台(让学习变的简单)
博文笔记

java.lang.String.isEmpty()的用法

创建时间:2016-03-03 投稿人: 浏览次数:3313

首先,来看isEmpty()的定义:

Declaration

Following is the declaration for java.lang.String.isEmpty() method

public boolean isEmpty()

Parameters

  • NA

Return Value

This method returns true if length() is 0, else false.

Exception

  • NA

也就是说,isEmpty()方法只负责判断字符串的长度是否为0,也就是针对""的情况。如果字符串为Null,其返回为false。


1.在实际情况中,常有条件:

if(!str.isEmpty()){

//如果字符串长度>0,则...

}

但是,在无法判断字符串是否为null的时候,应当完善上述条件:

if(str!=null && !str.isEmpty()){

//如果字符串长度>0,则...
}


2.对于"".equals(str),则equals will check its argument and return false if it"s null。用法和isEmpty()是一样的。

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。