isEmpty()如果当前字符串的长度为0,则String类的方法返回true。
import java.util.Scanner;
public class StringEmptyOrNull {
public static void main(String[] args) {
System.out.println("Enter a string value ::");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if(str.isEmpty()&& str==null ){
System.out.println("Given string is empty or null");
}else{
System.out.println("Given string is not empty or null");
}
}
}Enter a string value :: Hi welcome to nhooo Given string is not empty or null
Enter a string value :: Given string is empty or null