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)代码>
更新一点解释
如果查看codeString.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)代码>
更新一点解释
如果查看codeString.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
如果我们想知道,我们可以查看源代码。如果字符串长度为零,StringisEmpty
方法将返回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。如果我们想知道,我们可以查看源代码。如果字符串长度为零,StringisEmpty
方法将返回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一样