Java 如果(String!=null&;!String.isEmpty())不用于检查null

Java 如果(String!=null&;!String.isEmpty())不用于检查null,java,android,string,Java,Android,String,我正在检查字符串是否为空,但我不知道为什么它总是输入到if block Log.d(TAG,"String ID "+str); if (str!= null && !str.isEmpty()){ Log.d(TAG,"String ID is not Null "); } else { Log.d(TAG,"String ID is Null "); } 我还生成日志来检查字符串值,然后再检

我正在检查字符串是否为空,但我不知道为什么它总是输入到
if block

 Log.d(TAG,"String ID  "+str);
 if (str!= null && !str.isEmpty()){
    Log.d(TAG,"String ID is not Null ");                 
 } else {
    Log.d(TAG,"String ID is Null ");            
 }
我还生成日志来检查字符串值,然后再检查条件是否满足,它显示如下

01-06 07:17:46.128 30321-30376/com.example D/### String Checker ###: Page ID  null
01-06 07:17:46.128 30321-30376/com.example D/### String Checker ###: Page ID is not Null 
String str = null;
new AsyncTask(searchInterfaceChat,getActivity(),String.valueOf(str)).execute(query);
更新: 将字符串设置为
null
,然后像这样调用其他类

01-06 07:17:46.128 30321-30376/com.example D/### String Checker ###: Page ID  null
01-06 07:17:46.128 30321-30376/com.example D/### String Checker ###: Page ID is not Null 
String str = null;
new AsyncTask(searchInterfaceChat,getActivity(),String.valueOf(str)).execute(query);

为什么不使用TextUtils.isEmpty(),它将检查您的两种情况

 if (!TextUtils.isEmpty("your string")){
Log.d(TAG,"String ID is not Null ");                 
 } else {
   Log.d(TAG,"String ID is Null ");            
    } 
还要传递字符串,因为在本例中,您正在使用th string类

尝试使用字符串s=null和s=“ad”值在两种情况下都可以正常工作:

 public class Main
 {
public static void main(String[] args) {
 String str ="ss";
 if (str!=null&&!str.isEmpty()){
System.out.println("not Null");               
} else {
      System.out.println(" Null");               
    }
}

为什么不使用TextUtils.isEmpty(),它将检查您的两种情况

 if (!TextUtils.isEmpty("your string")){
Log.d(TAG,"String ID is not Null ");                 
 } else {
   Log.d(TAG,"String ID is Null ");            
    } 
还要传递字符串,因为在本例中,您正在使用th string类

尝试使用字符串s=null和s=“ad”值在两种情况下都可以正常工作:

 public class Main
 {
public static void main(String[] args) {
 String str ="ss";
 if (str!=null&&!str.isEmpty()){
System.out.println("not Null");               
} else {
      System.out.println(" Null");               
    }
}

在查看日志时,字符串的值似乎为“null”。您的字符串不是空的。检查一下。此观察仅适用于您的日志


更新代码后,问题是
String.valueOf(str)
,它正在将null转换为实际上是字符串的“null”。仅传递
str
,而不是
String.valueOf(str)
, 像
newasynctask(searchInterfaceChat,getActivity(),str)


更新一点解释

如果查看code
String.valueOf()
,如果对象为null,则返回
“null”
而不是
null
。因此,对于您来说,实际上是
“null”
被正确地打印在日志上,但会造成混淆。下面是代码

/**
 * Returns the string representation of the {@code Object} argument.
 *
 * @param   obj   an {@code Object}.
 * @return  if the argument is {@code null}, then a string equal to
 *          {@code "null"}; otherwise, the value of
 *          {@code obj.toString()} is returned.
 * @see     java.lang.Object#toString()
 */
public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

您可以阅读不准确的QA,但您可以了解
“null”
null

在查看日志时,字符串的值似乎是“null”。您的字符串不是空的。检查一下。此观察仅适用于您的日志


更新代码后,问题是
String.valueOf(str)
,它正在将null转换为实际上是字符串的“null”。仅传递
str
,而不是
String.valueOf(str)
, 像
newasynctask(searchInterfaceChat,getActivity(),str)


更新一点解释

如果查看code
String.valueOf()
,如果对象为null,则返回
“null”
而不是
null
。因此,对于您来说,实际上是
“null”
被正确地打印在日志上,但会造成混淆。下面是代码

/**
 * Returns the string representation of the {@code Object} argument.
 *
 * @param   obj   an {@code Object}.
 * @return  if the argument is {@code null}, then a string equal to
 *          {@code "null"}; otherwise, the value of
 *          {@code obj.toString()} is returned.
 * @see     java.lang.Object#toString()
 */
public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

您可以阅读不确切的QA,但您可以了解
“null”
null

如果我们想知道,我们可以查看源代码。如果字符串长度为零,String
isEmpty
方法将返回true

    public boolean isEmpty() {
        // Android-changed: Get length from count field rather than value array (see above).
        // Empty string has {@code count == 0} with or without string compression enabled.
        // return value.length == 0;
        return count == 0;
    }

如果对象为null,则方法
String.valueOf
将返回“null”

/**
     * @return  if the argument is {@code null}, then a string equal to
     *          {@code "null"}; otherwise, the value of
     *          {@code obj.toString()} is returned.
     * @see     java.lang.Object#toString()
     */
 public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

因此,当您在其他类中使用它时,它返回字符串NOTNULL。

如果我们想知道,我们可以查看源代码。如果字符串长度为零,String
isEmpty
方法将返回true

    public boolean isEmpty() {
        // Android-changed: Get length from count field rather than value array (see above).
        // Empty string has {@code count == 0} with or without string compression enabled.
        // return value.length == 0;
        return count == 0;
    }

如果对象为null,则方法
String.valueOf
将返回“null”

/**
     * @return  if the argument is {@code null}, then a string equal to
     *          {@code "null"}; otherwise, the value of
     *          {@code obj.toString()} is returned.
     * @see     java.lang.Object#toString()
     */
 public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

因此,当您在其他类中使用它时,它将返回字符串NOTNULL。

首先使用null,这样它就不会给出错误

str
不是
null
时就可以了

但是

如果
str
null

然后你写
str=null
它将首先尝试读取
str
,但它是
null
所以它会给出一个错误

if (null!=str  && !str.isEmpty()){
   Log.d(TAG,"String ID is not Null ");                 
} else {
   Log.d(TAG,"String ID is Null ");            
}

首先将null带入,这样它就不会出现错误

str
不是
null
时就可以了

但是

如果
str
null

然后你写
str=null
它将首先尝试读取
str
,但它是
null
所以它会给出一个错误

if (null!=str  && !str.isEmpty()){
   Log.d(TAG,"String ID is not Null ");                 
} else {
   Log.d(TAG,"String ID is Null ");            
}


您正在传递字符串值null,因为这是
String.valueOf(str)将返回的值
str
null
时,传递的字符串值为null,因为这是
String.valueOf(str)返回的值
str
null

时,这是实际的代码吗?字符串的值是多少?@ScaryWombat只是为了表明它是要避免的字符串confusion@ButI'mNotAWrapperClass检查更新值可能是
str
的值是
null
这是实际代码吗?字符串的值是多少?@ScaryWombat只是表明它是要避免的字符串confusion@ButI'mNotAWrapperClass检查更新值可能是
str
的值是
null
我知道这种方式,但我需要知道为什么我会进入if block我知道这种方式,但我需要知道为什么我会进入if block在
if
页面ID为null
之前的输出,所以我认为你的第二个假设是不正确的。你的字符串的值似乎是“null”-是的,根据我在主要问题下面的评论。你能告诉我这背后的原因吗,即使在日志中它说字符串是null没有日志说“null”不是null。理解两者之间的区别。null表示没有内存的字符串,但“null”表示字符串不是空的,它的值为“null”。if
前面的输出是
页面ID null
,因此我认为您的第二个假设不正确。字符串的值似乎为“null”-是的,根据我在主要问题下面的评论。你能告诉我这背后的原因吗?即使在日志中它说字符串为null。没有日志说“null”而不是null。理解两者之间的区别。null表示没有内存的字符串,但“null”表示字符串不是空的,它的值为“null”。您能告诉我这背后的原因吗?即使在日志中它说字符串为空,日志也只是打印字符串值
null
-就像您声明
Stri一样