Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification about creating arrays.

 
Hal Videira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I have been sent to this forum head first.



On line 2, my understanding is that a new array with space for 3 Dog reference variables is created. Then the array is assigned to the variable myDog which is of type Dog. Is this correct?

Many Thanks,

Hal

 
Antany Vasanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hal,

Welcome to Java Ranch..

If "dog1" is instance of Dog then the code is correct.

Regards,
Antany


 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hal Videira wrote:On line 2, my understanding is that a new array with space for 3 Dog reference variables is created. Then the array is assigned to the variable myDog which is of type Dog. Is this correct?


Welcome to JavaRanch!

You are almost correct:
"a new array with space for 3 Dog reference variables is created"
Correct

"Then the array is assigned to the variable myDog"
Correct

"myDog ... is of type Dog."
Incorrect. The type of myDog is Dog Array, or Dog[]. If it were of type Dog, for instance, you would be able to do this:

Since both yourDog and myDog would be the same type. But that would fail.

myDog is of type Dog Array, which holds references of type Dog.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this correct?

Almost. Except that the array is assigned to the variable myDogs which is of type array of Dog.
But you've got the main point.
 
Hal Videira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up, and also for the greetings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic