What is the default initialization of an array in Java?

后端 未结 7 828
我在风中等你
我在风中等你 2020-11-22 16:30

So I\'m declaring and initializing an int array:

static final int UN = 0;
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
    arr[i] = UN;
}
<         


        
7条回答
  •  执笔经年
    2020-11-22 16:42

    Thorbjørn Ravn Andersen answered for most of the data types. Since there was a heated discussion about array,

    Quoting from the jls spec http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5 "array component is initialized with a default value when it is created"

    I think irrespective of whether array is local or instance or class variable it will with default values

提交回复
热议问题