array in java_Objects in array in Java

可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:

问题:

I'm new to java, I was reading Head first Java when I came across object in arrays. The code is from the book itself, I get the flow of the code but I don't really understand the second line. What does new Dog[7] do, in the book it says

"Declare and Create a Dog array to hold 7 Dog references"

If we have already created dog references why do we need to create a dog reference for individual array items again.

Dog[] pets;

pets = new Dog[7];

pets[0] = new Dog();

回答1:

Before the second line, you just say that the variable pets exists, and it's an array of Dog, but the array doesn't exist in memory, because it hasn't been created.

In order to be able to use the array and read / store values inside, you need to actually create this array in memory, which is what the 2nd line does: it creates an array of Dog, of size 7.

回答2:

The first line declares a variable of a "Dog array" type. The second line actually initializes it with an array that has seven slots, each of which are null. The third line creates an actual Dog instance, and assigns it to the first slot of the array.

回答3:

Dog[] pets;

This will declare an array of dog. But when you do pets = new Dog[7]; it will initialize array with length of 7.

pets[0] = new Dog();

this statement will store an object of dog at 0th position.

回答4:

This line pets = new Dog[7]; creates a Array Object which will contain Dog objects.

So initially all 7 indeces in pets array are null. Therefore pets[0] = new Dog(); required to create Dog Objects

回答5:

Dog[] pets;

pets = new Dog[7];

pets[0] = new Dog();

In the second line, you are creating the array of reference as you mentioned. The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object. The second line actually initializes pets with an array that has seven slots, each of which are null. The third line creates an actual Dog instance, and assigns it to the first slot of the array. For more details and further understanding, you can refer to the official documentation of Oracle: https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.3.1

回答6:

There is a difference between declaration and initialization.

Dog[] pets declares that the variable pets is an array of Dogs

pets = new Dog[7] initializes the variable pets, it assigns it a value. The value is an array of size 7 full of null references.

It is the same for primitives :

int i; //declaration

i = 5; //initialization

As well as you can write

int i = 5;

you can write

Dog[] pets = new Dog[7];

In this case, you do the declaration and initialization on the same line.

回答7:

With that you are declaring and creating a java array able to contain 7 items of dog type. From a machine stand point you are creating a pointer to the memory space to contain your objects.

回答8:

Dog[] pets; : decalres the array of Dog

pets = new Dog[7]; : Creates an array (with contiguous memory allocation on heap) of Dog to hold 7 Dog references (initially null).

回答9:

Every object in java must be initialized with the keyword new, which allocates memory on the heap.

Arrays in java are also objects, thus they must be initialized with new.

By default, every object in the array will have a null value. So you must initialize every individual item with the new keyword again.

Situation at each step:

Dog[] pets; //

pets = new Dog[7]; //

pets[0] = new Dog(); //

See the oracle tutorial for arrays

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值