- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
据我们所知,Java 对象的字段是这样初始化的:
false
0
null
我的问题是哪种机制(方法)正在执行这项工作以及何时执行?
最佳答案
这些初始化是在 JVM 创建类的新实例时完成的。参见 Creation of new class instances部分 。它指出:
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (§8.3).
If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§4.12.5).
每种实例变量的默认值如下:
- For type
byte
, the default value is zero, that is, the value of(byte)0
.- For type
short
, the default value is zero, that is, the value of(short)0
.- For type
int
, the default value is zero, that is,0
.- For type
long
, the default value is zero, that is,0L
.- For type
float
, the default value is positive zero, that is,0.0f
.- For type
double
, the default value is positive zero, that is,0.0d
.- For type
char
, the default value is the null character, that is,'\u0000'
.- For type
boolean
, the default value isfalse
.- For all
reference types
(§4.3), the default value isnull
.
关于java - 哪种机制负责 Java 中字段的初始初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772378/
我新发现了 Perl fork ,我非常喜欢。但有一件事让我担心——如果我只是左右分离进程,这肯定会在某个地方引起一些问题。是否应该使用一种合理的检查来确保我的小应用程序不会占用我机器的所有资源? 拿
谁负责 MVVM 中的异步数据获取?例如,我的 View 有 5 个不同的数据集合要获取,我希望按预定义的顺序异步检索这些数据。为此,我使用协程和任务。 问题:VM 是否对此负责,我的模型/存储库应该
我有什么 ember-cli POD 结构。我有嵌套的路由,因此有以下文件夹结构: | |_pods |_items |_index | |_
我有一个 C# WPF 应用程序,它从数据库中读取数据然后做一些工作。但问题是当我读取数据时,我的 UI 没有响应。我已经尝试过任务和调度程序。他们都没有帮助。下面的代码在 button_click
在使用 /sys/class/leds/ 摆弄 LED 之后,我现在正试图了解如何使用 linux kernel power supply attributes 控制计算机中的电池。 ,特别是 CHA
我在我的桌面应用程序中做了一些非常简单的错误记录,该应用程序通过 SerialPort 与设备通信。我做的一件事是设置一个全局异常捕获器,它除了使用以下方法记录堆栈跟踪外什么都不做: AppDomai
我是一名优秀的程序员,十分优秀!