purpose of array with no subscript

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
anderijk1
Posts: 4
Joined: Sat Mar 22, 2014 11:33 am
Contact:

purpose of array with no subscript

Post by anderijk1 »

I don't understand how this answer will not cause a compile or runtime problem. When is it okay to assign an array element to an array that has no subscript?

daa = daaa[0];

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: purpose of array with no subscript

Post by admin »

An assignment is ok only when the object you are assigning is of the same type as the type of the reference to which you are assigning.

So if daaa[0] refers to an array of the same dimensions as daa, then the assignment is ok. For example, if you have:
int[] ia = new int[]{ 1 };
int x = ia[0]; //here ia[0] and x are of same type i.e. int so it is ok.

int[][] iaa = new int[1][1]{ {1} };
ia = iaa[0]; here iaa is an array of array of ints but iaa[0] is an array of ints which is the same type as ia, so this is ok as well.

x = iaa[0]; this is not ok because x is not an array of ints, it is int.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

anderijk1
Posts: 4
Joined: Sat Mar 22, 2014 11:33 am
Contact:

Re: purpose of array with no subscript

Post by anderijk1 »

Thank you very much for the excellent explanation.

Post Reply

Who is online

Users browsing this forum: No registered users and 112 guests