字符串对比的问题

版权声明:本文为原创文章,未经允许不得转载 https://blog.csdn.net/qq_33875256/article/details/89916326

在字符串进行对比的时候,=并不是对比两边的字符串,而是对比两边的对象类型,如

if(String s = ""){}

判断的是s为字符串,""为字符串等式即成立。

如果想判断字符串是否相等应该用

String s = "";
s.equals("");

判断是否为可以用

String s = "";
s.isEmpty();

或者用

  String s = "";
  StringUtils.isEmpty(s);

它内部判断的是

public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

猜你喜欢

转载自blog.csdn.net/qq_33875256/article/details/89916326
今日推荐