Java中数组的默认初始化是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3426843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:41:27  来源:igfitidea点击:

What is the default initialization of an array in Java?

javaarraysinitialization

提问by Hristo

So I'm declaring and initializing an int array:

所以我声明并初始化一个 int 数组:

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

Say I do this instead...

说我这样做...

int[] arr = new int[5];
System.out.println(arr[0]);

... 0will print to standard out. Also, if I do this:

...0将打印到标准输出。另外,如果我这样做:

static final int UN = 0;
int[] arr = new int[5];
System.out.println(arr[0]==UN);

... truewill print to standard out. So how is Java initializing my array by default? Is it safe to assume that the default initialization is setting the array indices to 0which would mean I don't have to loop through the array and initialize it?

...true将打印到标准输出。那么默认情况下 Java 是如何初始化我的数组的呢?假设默认初始化是将数组索引设置为0这意味着我不必遍历数组并初始化它是否安全?

Thanks.

谢谢。

采纳答案by Thorbj?rn Ravn Andersen

Everything in a Java program not explicitly set to something by the programmer, is initialized to a zero value.

Java 程序中未由程序员显式设置的所有内容都被初始化为零值。

  • For references (anything that holds an object) that is null.
  • For int/short/byte/long that is a 0.
  • For float/double that is a 0.0
  • For booleans that is a false.
  • For char that is the null character '\u0000'(whose decimal equivalent is 0).
  • 对于引用(任何包含对象的东西),它是null.
  • 对于 int/short/byte/long,这是一个0.
  • 对于 float/double 是一个 0.0
  • 对于布尔值,它是一个false.
  • 对于 char 是空字符'\u0000'(其十进制等效值为 0)。

When you create an array of something, all entries are also zeroed. So your array contains five zeros right after it is created by new.

当您创建一个数组时,所有条目也会被清零。因此,您的数组在创建后立即包含五个零new

Note (based on comments): The Java Virtual Machine is not required to zero out the underlying memory when allocating local variables (this allows efficient stack operations if needed) so to avoid random values the Java Language Specification requires local variables to be initialized.

注意(基于注释):Java 虚拟机在分配局部变量时不需要将底层内存清零(如果需要,这允许有效的堆栈操作),因此为了避免随机值,Java 语言规范要求初始化局部变量。

回答by Dave Costa

From the Java Language Specification:

来自Java 语言规范

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

    • 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 is false.
    • For all reference types (§4.3), the default value is null.
  • 每个类变量、实例变量或数组组件在创建时都使用默认值进行初始化(第 15.9 节、第 15.10 节):

    • 对于 byte 类型,默认值为零,即 的值(byte)0
    • 对于 short 类型,默认值为零,即 的值(short)0
    • 对于 int 类型,默认值为零,即0.
    • 对于 long 类型,默认值为零,即0L.
    • 对于 float 类型,默认值为正零,即0.0f
    • 对于 double 类型,默认值为正零,即0.0d
    • 对于 char 类型,默认值为空字符,即'\u0000'.
    • 对于类型 boolean,默认值为false
    • 对于所有引用类型(第 4.3 节),默认值为null

回答by Aniket Thakur

JLSclearly says

JLS明确表示

An array initializer creates an array and provides initial values for all its components.

数组初始值设定项创建一个数组并为其所有组件提供初始值。

and this is irrespective of whether the array is an instance variable or local variable or class variable.

这与数组是实例变量还是局部变量或类变量无关。

Default values for primitive types : docs

原始类型的默认值:docs

For objects default values is null.

对于对象,默认值为null

回答by Abhishek Singh

According to java,

根据java,

Data Type - Default values

byte - 0

short - 0

int - 0

long - 0L

float - 0.0f

double - 0.0d

char - '\u0000'

String (or any object) - null

boolean - false

数据类型 - 默认值

字节 - 0

短 - 0

整数 - 0

长 - 0L

浮动 - 0.0f

双 - 0.0d

字符 - '\u0000'

字符串(或任何对象)- null

布尔值 - 假

回答by RAJIV GUPTA

Java says that the default length of a JAVA array at the time of initialization will be 10.

Java 说初始化时 JAVA 数组的默认长度为 10。

private static final int DEFAULT_CAPACITY = 10;

But the size()method returns the number of inserted elements in the array, and since at the time of initialization, if you have not inserted any element in the array, it will return zero.

但是该size()方法返回的是数组中插入元素的个数,因为在初始化的时候,如果你还没有在数组中插入任何元素,它会返回零。

private int size;

public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

public void add(int index, E element) {
    rangeCheckForAdd(index);
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,size - index);
    elementData[index] = element;
    size++;
}

回答by kkk

Every class in Java have a constructor ( a constructor is a method which is called when a new object is created, which initializes the fields of the class variables ). So when you are creating an instance of the class, constructor method is called while creating the object and all the data values are initialized at that time.

Java 中的每个类都有一个构造函数(构造函数是一个在创建新对象时调用的方法,它初始化类变量的字段)。因此,当您创建类的实例时,会在创建对象时调用构造函数方法,并在那时初始化所有数据值。

For object of integer array type all values in the array are initialized to 0(zero) in the constructor method. Similarly for object of boolean array, all values are initialized to false.

对于整数数组类型的对象,数组中的所有值都在构造函数方法中初始化为 0(零)。同样对于布尔数组的对象,所有值都初始化为 false。

So Java is initializing the array by running its constructor method while creating the object

所以Java通过在创建对象时运行其构造函数方法来初始化数组

回答by nantitv

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

Thorbj?rn Ravn Andersen 回答了大多数数据类型。既然有关于数组的热烈讨论,

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"

引用 jls 规范http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5“数组组件在创建时使用默认值进行初始化”

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

我认为无论数组是本地变量还是实例变量或类变量,它都会使用默认值