变量默认值 [英] Variable default value
问题描述
重要的是要指出模板的内容字段默认设置为null(因为Java在创建时与所有未初始化的对象字段一起使用)。
"It is important to point out that the content field of the template is by default set to null (as Java does with all noninitialized object fields upon creation)."
来自书籍JavaSpaces Principles Patterns and Practice
It is from book "JavaSpaces Principles Patterns and Practice"
这是代码:
public class Message implements Entry {
public String content;
public Message() {
}
}
我想知道如果这是真的,因为我在网上某处看到这不是真的吗?
I wonder if this is true, because I watched somewhere on internet that this is not true?
推荐答案
是的,这是真的,但它可能并不意味着你认为它意味着什么。如果未指定任何值,则所有对象字段将初始化为 null
,但基本类型具有其他默认值。例如, int
字段默认为 0
, float
s到 0.0
, boolean
s到 false
。
Yes, this is true, but it may not mean quite what you think it means. All object fields will be initialized to null
if no value is specified, but primitive types have other default values. For instance, int
fields default to 0
, float
s to 0.0
, and boolean
s to false
.
这里有关于这些默认值的更多信息: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 。
More information on these defaults here: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html.
这篇关于变量默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!