null 和空 ("") Java 字符串之间的区别
null
和 ""
(空字符串)有什么区别?
我编写了一些简单的代码:
String a = "";
String b = null;
System.out.println(a == b); // false
System.out.println(a.equals(b)); // false
两个语句都返回false
。看来,我无法找到它们之间的实际区别是什么。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
String s=null;
字符串未初始化为 null。如果尝试任何字符串操作,它可能会抛出空指针异常
String t="null";
它是一个字符串文字,其值字符串为“null”,与 t = “xyz”相同。它不会抛出空指针。
String u="";
它是作为空字符串,不会抛出空指针。
String s=null;
String is not initialized for null. if any string operation tried it can throw null pointer exception
String t="null";
It is a string literal with value string "null" same like t = "xyz". It will not throw null pointer.
String u="";
It is as empty string , It will not throw null pointer.
简单来说,
"" 是一个空的字符串
null 是一个空的字符串变量< /strong>.
In simple term,
"" is an empty String
null is an empty String Variable.
字符串可以为空或具有
null
值。如果字符串为null
,则它不引用内存中的任何内容。尝试s.length()>0
。这是因为如果字符串为空,它仍然返回长度 0。因此,如果您不输入任何内容,那么它仍然会继续循环,因为它不会将字符串注册为null
。而如果您检查长度,那么它将退出循环。A string can be empty or have a
null
value. If a string isnull
, it isn't referring to anything in memory. Trys.length()>0
. This is because if a string is empty, it still returns a length of 0. So if you enter nothing for the same, then it will still continue looping since it doesn't register the string asnull
. Whereas if you check for length, then it will exit out of it's loop.“我称之为我的十亿美元错误。这是 1965 年空引用的发明” - https: //en.wikipedia.org/wiki/Tony_Hoare
对于现实世界,可以假设两者相同。它只是一种编程语言的语法,它在两者之间造成了差异,正如其他人在这里所解释的那样。这只会产生开销,例如在检查/比较字符串变量是否有某些内容时,您必须首先检查它是否不为空,然后进行实际字符串比较,即两次比较。这对于每次字符串比较来说都是处理能力的浪费。
Objects.equals() 在调用 .equals() 之前检查是否为 null。
"I call it my billion-dollar mistake. It was the invention of the null reference in 1965" - https://en.wikipedia.org/wiki/Tony_Hoare
With respect to real world both can be assumed same. Its just a syntax of a programming language that creates a difference between two as explained by others here. This simply creates overhead like when checking/comparing whether string variable has something, you have to first check if its not null and then actual string comparing ie two comparisons. This is a waste of processing power for every string comparisons.
Objects.equals() checks for null before calling .equals().
null 和 null 之间的区别空字符串。例如:您有一个名为 x 的变量。如果你用JS编写,
var x = "";
这意味着你分配了一个空字符串(长度为0)的值。实际上,这就像某种东西,但感觉什么都没有:) 另一方面,
var y = null;
这意味着你还没有为 y 分配一个值,这通过在 y 中写入 null 来明确表示声明时间。如果你写 y.length;它将抛出一个错误,表明没有为 y 分配任何值,因此无法读取 y 的长度。
Difference between null & empty string. For example: you have a variable named x. If you write in JS,
var x = "";
this means that you have assigned a value which is empty string (length is 0). Actually this is like something but which is feel of nothing :) On the other hand,
var y = null;
this means you've not assigned a value to y that clearly said by writing null to y at the time of declaration. If you write y.length; it will throw an error which indicates that no value assigned to y and as a result can't read length of y.
当你写成
String a = "";
时,意味着有一个字符串类型的变量'a',它指向字符串池中的一个对象引用,其值为“”。由于变量
a
持有一个有效的字符串对象引用,所以字符串的所有方法都可以在这里应用。而当您编写
String b = null;
时,这意味着有一个字符串类型的变量
b
指向一个未知引用。对未知引用的任何操作都将导致 NullPointerException。现在,让我们评估以下表达式。
When you write
String a = "";
It means there is a variable 'a' of type string which points to a object reference in string pool which has a value "". As variable
a
is holding a valid string object reference, all the methods of string can be applied here.Whereas when you write
String b = null;
It means that there is a variable
b
of type string which points to an unknown reference. And any operation on unknown reference will result in anNullPointerException
.Now, let us evaluate the below expressions.
这个概念可以从数学中更好地理解。您是否尝试过使用计算器(例如 7/0)将数字(非零)除以 0?您将得到如下所示的结果:
undefined
、not a number
、null
等。这意味着该操作是不可能的,因为一些原因(让我们改天再讨论这些原因)。现在,执行以下操作:0/7。您将得到输出 0。这意味着该操作是可能的并且可以执行,但您的答案只是 0,因为除法后什么都没有留下。存在有效输出且该输出为零。
在第一个示例中,不仅输出无效,而且操作也无法执行。这类似于 java 中的
null
字符串。第二个示例类似于空
字符串。This concept can be better understood from mathematics. Have you ever tried dividing a number (not zero) by 0 using a calculator e.g 7/0? You will get a result that looks like something this:
undefined
,not a number
,null
etc. This means that the operation is impossible, for some reasons (let's leave those reasons to be discussed another day).Now, perform this: 0/7. You will get the output, 0. This means that the operation is possible and can be executed, but you the answer is just 0 because nothing is left after the division. There is a valid output and that output is zero.
In the first example, not only was the output invalid, the operation was not possible to execute. This is akin to
null
string in java. The second example is akin toempty
string.令人惊奇的答案,但我想从不同的角度给出。
所以这可以告诉我们“”和null指向不同的对象引用。
Amazing answers, but I'd like to give from a different perspective.
So this can tell us "" and null point to the different object references.
作为一种好奇心
as a curiosity
null 表示名称未引用任何实例化对象。 "" 表示空字符串。
这里 a 引用了某个恰好是空字符串的对象。 b 没有引用任何对象,因为它为空。
null means the name isn't referencing any instantiated object. "" means an empty string.
Here a is referencing some object which happens to be an empty string. b isn't referencing any object as it's null.
这里
a是一个对象
,但b(null)
不是一个对象,它是一个空引用这是我的类似答案
here
a is an Object
butb(null)
is not an Object it is a null referencehere is my similar answer
此图像可能会帮助您理解差异。
该图像是从 ProgrammerHumor 收集的
This image might help you to understand the differences.
The image was collected from ProgrammerHumor
两者之间存在相当显着的差异。空字符串
""
是“其中没有字符的字符串”。它是一个具有明确长度的实际字符串。所有标准字符串操作都在空字符串上明确定义 - 您可以将其转换为小写,查找其中某些字符的索引等。空字符串null
是“没有字符串”根本没有。”它没有长度,因为它根本不是字符串。尝试对空字符串应用任何标准字符串操作都会在运行时导致NullPointerException
。There is a pretty significant difference between the two. The empty string
""
is "the string that has no characters in it." It's an actual string that has a well-defined length. All of the standard string operations are well-defined on the empty string - you can convert it to lower case, look up the index of some character in it, etc. The null stringnull
is "no string at all." It doesn't have a length because it's not a string at all. Trying to apply any standard string operation to the null string will cause aNullPointerException
at runtime.你的陈述告诉你的只是“”与 null 不同 - 这是真的。 "" 为空字符串; null 表示没有赋值。
尝试一下可能会更有启发性:
“”仍然是一个字符串,这意味着您可以调用它的方法并获取有意义的信息。 null 是一个空变量——实际上什么也没有。
What your statements are telling you is just that "" isn't the same as null - which is true. "" is an empty string; null means that no value has been assigned.
It might be more enlightening to try:
"" is still a string, meaning you can call its methods and get meaningful information. null is an empty variable - there's literally nothing there.
String 是一个对象,可以为 null
null 表示该 String 对象未实例化
"" 是实例化对象字符串的实际值,如 "aaa"
这里有一个链接可以澄清这一点 http://download.oracle.com/javase/tutorial/java/concepts/object.html
String is an Object and can be null
null means that the String Object was not instantiated
"" is an actual value of the instantiated Object String like "aaa"
Here is a link that might clarify that point http://download.oracle.com/javase/tutorial/java/concepts/object.html
“”是一个实际的字符串,尽管是一个空字符串。
然而,null 意味着 String 变量不指向任何内容。
a==b
返回 false,因为 "" 和 null 在内存中不占用相同的空间,换句话说,它们的变量不指向相同的对象。a.equals(b)
返回 false,因为“”显然不等于 null。不同之处在于,由于“”是一个实际的字符串,您仍然可以调用它的方法或函数,例如
a.length()
a.substring(0, 1)
等等。
如果字符串等于 null,如 b,如果您尝试调用,Java 将抛出 NullPointerException,例如:
b.length()
如果您想知道的差异是 ==与 equals 相比,它是这样的:
== 比较引用,就像如果我去
那会输出 false,因为我分配了两个不同的对象,并且 a 和 b 指向不同的对象。
但是,在这种情况下,
a.equals(b)
将返回 true,因为字符串的equals
将返回 true/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)">当 但警告说,Java 确实有字符串的特殊情况。
您可能会认为输出将为
false
,因为它应该分配两个不同的字符串。实际上,Java 会 intern 文字字符串(那些像我们示例中的 a 和 b 一样初始化)。所以要小心,因为这可能会给 == 的工作原理带来一些误报。"" is an actual string, albeit an empty one.
null, however, means that the String variable points to nothing.
a==b
returns false because "" and null do not occupy the same space in memory--in other words, their variables don't point to the same objects.a.equals(b)
returns false because "" does not equal null, obviously.The difference is though that since "" is an actual string, you can still invoke methods or functions on it like
a.length()
a.substring(0, 1)
and so on.
If the String equals null, like b, Java would throw a
NullPointerException
if you tried invoking, say:b.length()
If the difference you are wondering about is == versus equals, it's this:
== compares references, like if I went
That would output false because I allocated two different objects, and a and b point to different objects.
However,
a.equals(b)
in this case would return true, becauseequals
for Strings will return true if and only if the argument String is not null and represents the same sequence of characters.Be warned, though, that Java does have a special case for Strings.
You would think that the output would be
false
, since it should allocate two different Strings. Actually, Java will intern literal Strings (ones that are initialized like a and b in our example). So be careful, because that can give some false positives on how == works.您还可以这样理解 null 和空字符串之间的区别:
原始图片,作者:R. Sato (@raysato)
You may also understand the difference between null and an empty string this way:
Original image by R. Sato (@raysato)
在 Java 中,分配给
null
的引用类型根本没有值。分配给""
的字符串有一个值:空字符串,即其中没有字符的字符串。当变量被分配null
时,意味着不存在任何类型、字符串或其他类型的底层对象。In Java a reference type assigned
null
has no value at all. A string assigned""
has a value: an empty string, which is to say a string with no characters in it. When a variable is assignednull
it means there is no underlying object of any kind, string or otherwise."" 和 null 都是不同的。第一个意味着作为字符串变量声明的一部分,字符串常量已在字符串池中创建,并为其分配了一些内存。
但是当我们用 null 声明它时,它刚刚被实例化 jvm ,但没有为其分配内存。因此,如果您尝试通过使用 "" - 空白变量检查来访问此对象,则它无法防止 nullpointerexception 。请查找以下一种用例。
}
"" and null both are different . the first one means as part of string variable declaration the string constant has been created in the string pool and some memory has been assigned for the same.
But when we are declaring it with null then it has just been instantiated jvm , but no memory has been allocated for it. therefore if you are trying to access this object by checking it with "" - blank variable , it can't prevent nullpointerexception . Please find below one use-case.
}
对空字符串
""
的引用指向堆中的一个对象 - 因此您可以调用它的方法。但是指向
null
的引用在堆中没有指向的对象,因此您将得到一个NullPointerException
。A reference to an empty string
""
points to an object in the heap - so you can call methods on it.But a reference pointing to
null
has no object to point in the heap and thus you'll get aNullPointerException
.null
表示什么都没有;这意味着您从未为变量设置过值,但空意味着您已将""
值设置为String
。例如,请参阅以下示例:此处
str1
为null
表示您已定义它但尚未为其设置任何值。但是您已经定义了str2
并为其设置了一个空值,因此它具有一个值,即使该值为""
;str1
可以与其他字符串进行比较,但str2
比较时会在RuntimeException
中抛出NullPointerException
。null
means nothing; it means you have never set a value for your variable but empty means you have set""
value to yourString
. For instance, see the following example:Here
str1
isnull
meaning that you have defined it but not set any value for it yet. But you have definedstr2
and set an empty value for it so it has a value even though that value is""
;str1
can be compared with other strings butstr2
when compared will throwNullPointerException
inRuntimeException
.来自维基百科关于空字符串的文章。
From the wikipedia article on empty string.