Java null、0 和 nothing 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19653937/
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
What is the difference between null, 0 and nothing?
提问by Yvette
What is the difference between null
, 0
and nothing
?
之间有什么区别null
,0
和nothing
?
I cannot find a question addressing all three of these.
我找不到解决所有这三个问题的问题。
For example:
例如:
If I am getting input from a string
and parsing it to int
.
如果我从 a 获取输入string
并将其解析为int
.
return Integer.parseInt(sc.nextLine());
Or if I am asking if a string != ""
, or a is not nothing
I am confused about which to use when and why.
或者,如果我问是 astring != ""
还是 ais not nothing
我对何时以及为什么使用哪个感到困惑。
I am confused about which one to use when validating data.
我对验证数据时使用哪一个感到困惑。
采纳答案by Kevin Bowersox
null
means that a variable contains a reference to a space in memory that does not contain an object.
null
意味着变量包含对内存中不包含对象的空间的引用。
0
is a numeric data type with a value of 0.
0
是值为 0 的数值数据类型。
Nothing
doesn't really exist, however I think you may be viewing this as an empty String ""
which is simply a String
data type that does not contain a value.
Nothing
并不真正存在,但是我认为您可能将其视为一个空字符串""
,它只是一种String
不包含值的数据类型。
If your looking at this from a Javascript
paradigm it may be confusing. In Java variables are not truthy and falsey therefore the type is considered in comparisons.
如果您从Javascript
范式来看这可能会令人困惑。在 Java 中,变量不是真假,因此在比较中考虑类型。
回答by Dan D.
There isn't a "nothing" in Java. There is a null
reference, meaning that a variable refers to no object, so it is a kind of nothing, but there isn't the concept of undefined
as it is in Javascript.
Java 中没有“无”。有一个null
引用,意味着一个变量不指向任何对象,所以它是一种虚无,但没有undefined
像Javascript中那样的概念。
Also, please note that a String
comparison should be done using equals
, not !=
or ==
, so string.equals(otherString)
.
另外,请注意,String
比较应该使用equals
,而不是!=
或==
,所以string.equals(otherString)
。
The 0
value is the default value that an int
gets when declared as an instance variable.
该0
值是 anint
在声明为实例变量时获得的默认值。
回答by Ingo
0 is a number, you use it to check if a numeric value (int, short, float, double, etc.) is the number 0.
0 是一个数字,你可以用它来检查一个数值(int、short、float、double 等)是否是数字 0。
null
is the value of a reference that points nowhere, you use it to make sure a reference does indeed reference something.
null
是一个没有指向任何地方的引用的值,您可以使用它来确保引用确实引用了某些东西。
nothing
is not part of Java.
nothing
不是 Java 的一部分。
The closest thing for nothing (for: no information at all) is the void
declaration of a method. It states that the method returns literally nothing.
最接近无用的东西(因为:根本没有信息)是void
方法的声明。它指出该方法实际上不返回任何内容。
Note that 0, null
and the empty string ""
are values and thus contain information.
请注意,0null
和空字符串""
是值,因此包含信息。
For example, 0 is the answer to "What is 5 subtracted from 5?" (among others).
例如,0 是“从 5 中减去 5?”的答案。(除其他外)。
null
is the negative response to "Does this thing point to an object?".
null
是对“这东西指向一个对象吗?”的否定回答。
""
" could be the answer to "What is the longest sequenc of 'X's in your name?", unless your name is "Xanthippe".
""
”可能是“你名字中最长的‘X’序列是什么?”的答案,除非你的名字是“Xanthippe”。
回答by Foster Hao
For example:
例如:
int s; //s=null
int s = 0 ; //s=0
String s="";// s=nothing
回答by Nargis
"Null"
in Java , the object reference is not pointing to any object, it is null.
"Null"
在 Java 中,对象引用不指向任何对象,它为空。
"Nothing"
, Java has nothing called "Nothing"
"Nothing"
,Java没有什么叫做“Nothing”的
"0"
means that .the string object exists with a value of "0"
"0"
表示 .the 字符串对象存在,值为“0”
""
means an empty String ,i.e the String Object exists with value ""
""
表示一个空的 String ,即 String 对象存在的值为 ""
回答by dg_no_9
"nothing" means nothing in Java.
0 means an integer value greater than -1 and less than 1.
"Null" means if any Object Reference points nowhere in the memory, then it's called "Null" reference.
“无”在 Java 中没有任何意义。
0 表示大于 -1 且小于 1 的整数值。
“空”意味着如果任何对象引用在内存中无处指向,则称为“空”引用。
回答by alexgrimaldi
The best approach to check String objects in Java is using org.apache.commons.lang.StringUtils.
在 Java 中检查 String 对象的最佳方法是使用 org.apache.commons.lang.StringUtils。
If you do not want to use StringUtils
, you might want to check that sc.nextList()
is not null (!= null
) first of all, if it is not null then you can check that is different from "" (!= ""
).
如果您不想使用StringUtils
,您可能首先要检查它sc.nextList()
是否为空(!= null
),如果它不是空,那么您可以检查它是否与“”(!= ""
)不同。
回答by Kamran
Null variable isn't assigned any value. The memory space (of variable) is empty or holds garbage value. while a variable is 0, the variable holds the value '0'. The memory space has value '0' stored in it.
空变量未分配任何值。(变量的)内存空间为空或保存垃圾值。当变量为 0 时,该变量保持值 '0'。内存空间中存储了值“0”。
NULL basically means no value, or unknown,NULL value could be either empty, meaningless, or not even initialized. For eg when we say, Employee bonus is null, it means the person is not getting any bonus or not eligible for bonus.now in case if value is 0 its mean employee getting bonus but currently bonus is zero.
NULL 基本上意味着没有值,或者未知,NULL 值可能是空的、无意义的,或者甚至没有初始化。例如,当我们说,员工奖金为空时,这意味着该人没有获得任何奖金或没有资格获得奖金。现在,如果值为 0,则表示员工获得奖金但当前奖金为零。