null、0 和 nothing 有什么区别? [英] What is the difference between null, 0 and nothing?
问题描述
null
、0
和 nothing
有什么区别?
What is the difference between null
, 0
and nothing
?
我找不到解决所有这三个问题的问题.
I cannot find a question addressing all three of these.
例如:
如果我从 string
获取输入并将其解析为 int
.
If I am getting input from a string
and parsing it to int
.
return Integer.parseInt(sc.nextLine());
或者,如果我问的是 string != ""
还是 不是什么
,我很困惑何时以及为什么使用哪个.
Or if I am asking if a string != ""
, or a is not nothing
I am confused about which to use when and why.
我对在验证数据时使用哪一个感到困惑.
I am confused about which one to use when validating data.
推荐答案
null
表示变量包含对不包含对象的内存空间的引用.
null
means that a variable contains a reference to a space in memory that does not contain an object.
0
是数值为 0 的数据类型.
0
is a numeric data type with a value of 0.
Nothing
实际上并不存在,但是我认为您可能将其视为一个空字符串 ""
,它只是一个 String
不包含值的数据类型.
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.
如果您从 Javascript
范式来看这可能会令人困惑.在 Java 中,变量不是真假,因此在比较时会考虑类型.
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.
这篇关于null、0 和 nothing 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!