Can we guess the default array values ​​in Java? for example, suppose an int array is given for all zeros?

In practice, we can assume that all int arrays in Java will begin to be filled with zeros? for all machines running JVM?

Is this true for all types? char? boolean? transfers?

Where is it officially registered?

In the tutorials, I will say that int arrays are set to zero, but they also recommend writing a for loop to set all values ​​to zero, just "to be clearer."

+74
java
Oct 08 2018-10-10
source share
4 answers

The Java Language specification is a good place to look for such information:

The components of an array are unnamed variables that are created and initialized with default values ​​(§4.12.5) whenever a new object is created that is an array

The default values ​​are themselves given in section 4.12.5 .

  • For a byte type, the default value is zero, that is, the value (byte) is 0 .
  • For type short , the default value is zero, that is, the value (short) is 0 .
  • For the int type, the default value is zero, i.e. 0 .
  • For the long type, the default value is zero, that is, 0L .
  • For the float type, the default value is positive zero, i.e. 0.0f .
  • For the double type, the default value is positive zero, that is, 0.0d .
  • For type char , the default value is the null character, that is, '\ u0000' .
  • For the boolean type, the default value is false .
  • For all types of links, the default value is null .
+129
Oct 08 2018-10-10
source share
— -

Yes. Primitive types in Java are always zero-initialized. Links are also initialized to zero.

+17
Oct 08 2018-10-10
source share

It must be a Java language specification §4.12.5 Initial values ​​of variables . instead of §4.5.5

"For a byte of type, the default value is zero, that is, the value (byte) is 0.
For type short, the default value is zero, that is, the value (short) is 0.
For the int type, the default value is zero, i.e. 0.
For the long type, the default value is zero, that is, 0L.
For float, the default value is positive zero, i.e. 0.0f.
For the double type, the default value is positive zero, that is, 0.0d.
For the char type, the default value is the null character, i.e. '\ u0000'.
For the boolean type, the default value is false.
For all reference types (§4.3), the default value is null.

+7
Nov 06
source share

Your textbook is wrong! Writing such code is pointless and a waste of time typing it, and the computers that run it.

As others have said, the compiler ensures that variables outside the methods (class / instance variables) are set to 0, false, or null. Since you probably know that the compiler does not give variables inside the values ​​of the methods, but instead forces you to give them a value before using them.

You will find, if you do something “correctly,” that about 90% -95% of your “variables” never change after they are assigned a value. However, new programmers tend to do such things:

int x = 0; // some code that never uses x x = 5; // more code that only reads x and never modifies it. 

this does not allow you to mark x as "final". If you can mark x as "final" then this will prevent the value from being accidentally modified, which prevents errors.

I would write code like:

 final int x; // some code that never uses x x = 5; // more code that only reads x and never modifies it. 

This is called an empty ending (a finite variable that does not matter when declared).

If you remember that class / instance variables are initialized by the runtime, you hopefully will not write code to initialize them by dropping values, and then you can mark them as final.

My personal practice is to always mark everything as final until I find that I need to modify it and never initialize the variable until I know the actual value that I want to have. This makes the code faster (not noticeable, since assignments are usually very fast) and safer, as I rarely accidentally change the value when I shouldn't.

+4
Oct 08 2018-10-10
source share



All Articles