java String = null 和 String.isEmpty 之间有什么区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13689033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any difference between String = null and String.isEmpty?
提问by Danny
Is there any difference when using a if-statement to check if the string is empty by using String = null or String.isEmpty() ?
使用 if 语句通过使用 String = null 或 String.isEmpty() 检查字符串是否为空时有什么区别吗?
ie:
IE:
public String name;
if(name == null)
{
//do something
}
or
或者
public String name;
if(name.isEmpty())
{
//do something
}
if there is any different (including performance issues) please let me know.
如果有任何不同(包括性能问题),请告诉我。
回答by Mark Byers
The empty string is a string with zero length. The null value is not having a string at all.
空字符串是长度为零的字符串。空值根本没有字符串。
- The expression
s == null
will returnfalse
if s is an empty string. - The second version will throw a
NullPointerException
if the string is null.
- 如果 s 是空字符串,则表达式
s == null
将返回false
。 NullPointerException
如果字符串为空,第二个版本将抛出 a 。
Here's a table showing the differences:
这是一个显示差异的表格:
+-------+-----------+----------------------+
| s | s == null | s.isEmpty() |
+-------+-----------+----------------------+
| null | true | NullPointerException |
| "" | false | true |
| "foo" | false | false |
+-------+-----------+----------------------+
回答by Brian Agnew
The variable name
isn't a String. It's a referenceto a String.
变量name
不是字符串。它是对字符串的引用。
Hence the null check determines if name
actually referencesa String
. If it does, then (and only then) can you perform a further check to see if it's empty. i.e.
因此,空检查确定是否name
实际引用了 a String
。如果是,那么(并且只有这样)您才能执行进一步检查以查看它是否为空。IE
String name = null; // no string
String name = ""; // an 'empty' string
are two different cases. Note that if you don't check for nullness first, then you'll try and call a method on a null reference and that's when you get the dreaded NullPointerException
是两种不同的情况。请注意,如果您不先检查空性,那么您将尝试在空引用上调用一个方法,这就是您感到害怕的时候NullPointerException
回答by AbstractChaos
Strings that have assigned with "", don't contain any value but are empty (length=0), Strings that are not instantiated are null.
用“”赋值的字符串不包含任何值而是空的(长度=0),未实例化的字符串为空。
回答by Jigar Joshi
isEmpty()
checks for empty string ""
,
isEmpty()
检查空字符串""
,
it will throw NullPointerException
if you invoke isEmpty()
on null
instance
NullPointerException
如果您isEmpty()
在null
实例上调用它会抛出
回答by bellum
If you apply this code:
如果您应用此代码:
if(name.isEmpty())
{
//do something
}
when name
is null, you'll get NullPointerException
.
当name
为空时,你会得到NullPointerException
.
null
checking shows you whether there is an object generally.isEmpty
checking shows you whether the content of existingString
object is empty.
null
检查一般会显示是否有对象。isEmpty
检查显示现有String
对象的内容是否为空。
回答by popalka
Look at source code of your java version.
查看您的java版本的源代码。
For example, in openjdk-7: http://www.docjar.com/html/api/java/lang/String.java.html
例如,在 openjdk-7 中:http: //www.docjar.com/html/api/java/lang/String.java.html
119 /** The count is the number of characters in the String. */
120 private final int count;
663 /**
664 * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
665 *
666 * @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
667 * <tt>false</tt>
668 *
669 * @since 1.6
670 */
671 public boolean isEmpty() {
672 return count == 0;
673 }
回答by AlexWien
isEmpty checks for String "". Best practise is to check:
isEmpty 检查字符串“”。最佳做法是检查:
if (str != null && !str.isEmpty() {
// process string
}
回答by Patrick Vallée
I had this issue this week while modifying some old JaVa code and i learn here that i must always make all those check. The answer is indeed correct but i find it hard to remember each time so i decide to make a little function that do it for me in 1 simple call.
本周我在修改一些旧的 Java 代码时遇到了这个问题,我在这里了解到我必须始终进行所有这些检查。答案确实是正确的,但我发现每次都很难记住,所以我决定做一个小功能,在 1 个简单的调用中为我完成。
whit this, you always get the answer you want :
有了这个,你总能得到你想要的答案:
public boolean StringIsNull(String pi_sChaine)
{ boolean bTrueOrFalse = true;
if (pi_sChaine == null || pi_sChaine.isEmpty())
{ bTrueOrFalse = true; }
else
{ bTrueOrFalse = false; }
return bTrueOrFalse;
}