简体   繁体   English

在java中创建数组时,new运算符会做什么?

[英]What does the new operator do when creating an array in java?

When we create a object of a classtype the new operator allocates the memory at the run time. 当我们创建一个classtype对象时,new运算符会在运行时分配内存。

Say

myclass obj1 = new myclass();

here the myclass() defines a constructur of myclass 这里myclass()定义了myclass的构造函数

but

int arr1[4] = new int[];

new allocates the memory but, what the int[] does here? new分配内存,但int[]在这里做什么?

New allocates the memory but, what the int[] does here? 新的分配内存,但int []在这里做什么?

The int[] part simply specifies what type the newly allocated memory should have. int[]部分简单地指定新分配的内存应该具有的类型。

However, since an array can't grow dynamically, it doesn't make sense to write new int[] (even though you've specified int[4] in the declaration), you'll have to write it either like this 但是,由于数组不能动态增长,所以编写new int[]是没有意义的(即使你在声明中指定了int[4] ),你必须像这样编写它

int arr1[] = new int[4];

or like this 或者像这样

int arr1[] = {1, 2, 3, 4};

Relevant sections in the JLS: JLS中的相关部分:

Your code: 你的代码:

int arr1[4] = new int[];

will not compile. 不会编译。 It should be: 它应该是:

int arr1[] = new int[4];

putting [] before the array name is considered good practice, so you should do: 在数组名称之前放置[]被认为是一种很好的做法,所以你应该这样做:

int[] arr1 = new int[4];

in general an array is created as: 通常,数组创建为:

type[] arrayName = new type[size];

The [size] part above specifies the size of the array to be allocated. 上面的[size]部分指定要分配的数组的大小。

And why do we use new while creating an array? 为什么我们在创建数组时使用new

Because arrays in Java are objects. 因为Java中的数组是对象。 The name of the array arrayName in above example is not the actual array, but just a reference. 上面示例中的数组arrayName的名称不是实际的数组,而只是一个引用。 The new operator creates the array on the heap and returns the reference to the newly created array object which is then assigned to arrayName . new运算符在堆上创建数组,并返回对新创建的数组对象的引用,然后将其分配给arrayName

The above program declares anArray with the following line of code: 上面的程序使用以下代码行声明anArray:

int[] anArray; // declares an array of integers //声明一个整数数组

Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. 与其他类型的变量的声明一样,数组声明有两个组件:数组的类型和数组的名称。 An array's type is written as type[], where type is the data type of the contained elements; 数组的类型写为type [],其中type是包含元素的数据类型; the square brackets are special symbols indicating that this variable holds an array. 方括号是特殊符号,表示此变量包含数组。 The size of the array is not part of its type (which is why the brackets are empty). 数组的大小不是其类型的一部分(这就是括号为空的原因)。 As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type. 与其他类型的变量一样,声明实际上并不创建数组 - 它只是告诉编译器该变量将包含指定类型的数组。

int[] a=new int[5];
  1. In the above array declaration int[] is the array type ie integer type array 在上面的数组声明中,int []是数组类型,即整数类型数组
  2. a is the array name for example to declare a variable 'a'------> int a; a是数组名称,例如声明变量'a'------> int a; same way for arrays int[] a where [] is to indicate declaring array only diff is [] 对于数组int [] a的相同方式,其中[]表示声明数组只有diff是[]

now come to new 现在来到新的

new is an operator what is function of this operator? new是一个运算符这个运算符的功能是什么?

this new operator goes to heap memory and allocate an array (due to int[])and allocate 5 values inside array type int(due to int[5]) calls the reference variable.here reference variable is arrayname ie a. 这个新运算符转到堆内存并分配一个数组(由于int [])并在数组类型int中分配5个值(由于int [5])调用引用变量。其中引用变量是arrayname即a。

whatever array created in heap is given to reference variable a 在堆中创建的任何数组都被赋予引用变量a

                memory diagram
                ---------------------
                |  in heap memory    |
                |                    |
                ||------------------ |
                ||  |   |   |  |  |  |
                |------------------- |
      /          ---------------------
    /
  /   
a/

5 positions as int[5] in above example and reference variable point to array memory location 在上面的示例中,5个位置为int[5] ,引用变量指向数组内存位置

Creates an instance of the object. 创建对象的实例。 new allocates memory on heap . new堆上分配内存。 the int arr1[4]=new int[]; int arr1[4]=new int[]; allocates 14 bytes on heap. 在堆上分配14个字节。

The correct declaration of int array is like following: int数组的正确声明如下:

int[] arr = new int[] {1,2,3};  // legal, array of size 3
int[] a = new int[100];  // Declare and allocate, array of size 100

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

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