0

After reading this article I am confused about the 2nd step of the 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. 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.

It says that 2nd step initializes all the instance variables to their default value . In this case, firstly the JVM does this ?

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

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

Coffee

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

What does he exactly mean by 2nd Step ?

2
  • The 2nd step is what you described. What is your question? Commented Mar 20, 2015 at 14:07
  • This is the question , does really JVM work like that ? Why not just initializing directly to what mlVolume, temperature ... why not skipping the =0 part? Commented Mar 20, 2015 at 14:09

2 Answers 2

1

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. 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. 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.

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.

Sign up to request clarification or add additional context in comments.

5 Comments

but before initializing them to 1000 and 21.0f, the variables came as garbage ? or did the JVM initialized them to 0 and then to 1000 and 21.0f ?
They would be garbage before, but you have no way to access these variables before the VM initializes them to default values, so you don't have to worry about that (except maybe when you use some dirty introspection and reflection tricks).
I will ask in another way , when JVM sees this line of code : private int mlVolume = 1000; that memory chunk of mlVolume will come as a garbage value, next the JVM will assign the mlVolume to 0 and ONLY then it will assign it to 1000 ? In other words does the JVM do 2 initializations instead of 1 ?
@Oleg that would be an implementation detail, but I see no reason why any JVM would do that as it would be a pointless waste of CPU cycles.
I asked that because of the article , the author mentioned that.
1

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.