简体   繁体   English

Java实例变量初始化2次?

[英]Java Instance Variable initialized 2 times?

After reading this article I am confused about the 2nd step of the JVM. 看完文章我感到困惑的JVM的第二步。

 class Liquid { private int mlVolume; private float temperature; // in Celsius Liquid(int mlVolume, float temperature) { this.mlVolume = mlVolume; this.temperature = temperature; } //... } // In source packet in file init/ex18/Coffee.java class Coffee extends Liquid { private boolean swirling; private boolean clockwise; public Coffee(int mlVolume, float temperature, boolean swirling, boolean clockwise) { super(mlVolume, temperature); this.swirling = swirling; this.clockwise = clockwise; } 

When you instantiate a new Coffee object with the new operator, the Java virtual machine first will allocate (at least) enough space on the heap to hold all the instance variables declared in Coffee and its superclasses. 当您使用new运算符实例化一个新的Coffee对象时,Java虚拟机将首先在堆上分配(至少)足够的空间来容纳在Coffee及其超类中声明的所有实例变量。 Second , the virtual machine will initialize all the instance variables to their default initial values. 其次 ,虚拟机会将所有实例变量初始化为其默认初始值。 Third , the virtual machine will invoke the (init)/super constructor method in the Coffee class. 第三 ,虚拟机将在Coffee类中调用(init)/ super构造函数方法。

It says that 2nd step initializes all the instance variables to their default value . 它说第二步将所有实例变量初始化为其默认值。 In this case, firstly the JVM does this ? 在这种情况下,首先JVM会这样做吗?

Liquid 液体

this.mlVolume = 0;
this.temperature = 0

Coffee 咖啡

this.swirling = 0;
this.clockwise = 0;

and only after the Liquid(int, float) has been called it does this : 并且只有在调用Liquid(int,float)之后才这样做:

Liquid 液体

this.mlVolume = mlVolume;
this.temperature = temperature;

Coffee 咖啡

this.swirling = swirling;
this.clockwise = clockwise;

What does he exactly mean by 2nd Step ? 第二步他到底是什么意思?

Yes. 是。 By default every field is initialized to default value like: 默认情况下,每个字段都初始化为默认值,例如:

If you defined some object as a field, then it will be initialized to null while if its of int type then to 0 and for boolean, it will initialize to false and so on. 如果您将某个对象定义为字段,则将其初始化为null,如果其int类型则为0,对于布尔值,它将初始化为false,依此类推。

Reason it does is, it's not sure if you will have some initial value to your fields which you are going to initialize in constructor. 原因是,不确定是否要在构造函数中初始化的字段具有一些初始值。

When memory is allocated, that memory is usually not empty. 分配内存后,该内存通常不为空。 It is filled with whatever was there previously in memory. 它充满了内存中以前存在的任何内容。 So the first thing the JavaVM does after allocating memory, is to clean it up by overwriting everything with default values. 因此,JavaVM在分配内存之后要做的第一件事就是通过覆盖所有具有默认值的东西来清理它。 The "default default" value for most types is something equivalent to 0, but you can give a variable a different default value when you declare it. 大多数类型的“默认默认”值等于0,但是在声明变量时可以给它一个不同的默认值。 When your class would look like this: 当您的班级看起来像这样:

class Liquid {
    private int mlVolume = 1000;
    private float temperature = 21.0f; // in Celsius

the JavaVM would initialize them to reasonable default values of 1 liter and room temperature instead of 0 volume and freezing point. JavaVM会将它们初始化为1升和室温的合理默认值,而不是0体积和凝固点。

The author seems to come from a C/C++ background where initialization to 0 does not happen automatically and the programmer is responsible for making sure that all variables are set to known values before using them, because otherwise there could be anything in them. 作者似乎来自C / C ++背景,在这种背景下不会自动初始化为0,程序员负责确保在使用变量之前将所有变量都设置为已知值,否则它们中可能有任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM