Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Tue Jul 31, 2012 7:57 am
by JohnnyFanta
Can someone explain this question in more detail for me I'm a bit confused with the explanation given.
There is a subtle difference between: int[] i; and int i[]; although in both the cases, i is an array of integers.
The difference is if you declare multiple variables in the same statement such as: int[] i, j; and int i[], j;, j is not of the same type in the two cases.
In the first case, j is an array of integers while in the second case, j is just an integer.
Therefore, in this question: array1 is an array of int array2, array3, array4, and array5 are arrays of int arrays Therefore, option 1, 2 and 5 are valid.
Thanks
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Tue Jul 31, 2012 10:54 pm
by admin
The first thing is that for an assignment to work, the object on the right hand side (of = sign) that you are trying to assign to the variable on the left hand side should be compatible with the type of the variable.
For example, String s = new Integer(19); will not work because the type of the variable s is String but the object is Integer.
Similarly, in case of array variables, if the variable is of type "array of int", then on the right hand side you must have an array of int. An "array of array of int" is not compatible with an "array of int".
So, in this question, all you need to do is to first identify the type of the variable and make sure that the object that you are trying to assign to this variable is of the same type.
array1 = array2; is not valid because array1 is of type "array of int", while array2 is of type "array of array of int", which are not compatible with each other.
If you go through the explanation again and work out the other option now, I think you will get it.
Please let me know if you still have any confusion.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Mon Oct 22, 2012 6:42 am
by Guest
So this just means
You can put an int[] of arrays into an array but can't put an array into an int[] of arrays?

Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Mon Mar 11, 2013 2:00 pm
by lordnovas
Question: If array2[] is an Array of Arrays of Type int, does that make it a 2D Array?
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Mon Mar 11, 2013 2:35 pm
by admin
lordnovas wrote:Question: If array2[] is an Array of Arrays of Type int, does that make it a 2D Array?
Yes.
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Tue Mar 12, 2013 12:51 pm
by lordnovas
Awesome, thanks a bunch.
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Thu Jun 06, 2013 5:49 am
by Crashtest
To make it crystal clear I broken it into:
int[] array1, array2[];
is the same as
int[] array1;
int[] array2[]; // which is the same as int[][] array2;
Now the question does look easy

Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Fri Aug 16, 2013 8:33 am
by ewebxml
Can someone explain why
the assignment statements given in the problem work
"only if" you place the declarations of the arrays at the Class level and the assignments within main.
If you place the code given in the problem
1) At the class level or
2) within main
it will not compile.
Code: Select all
// The following code compiles
public class ArrayTest1 {
static int[] array1, array2[];
static int[][] array3;
static int[] array4[], array5[];
public static void main(String[] args) {
array2 = array3;
array2 = array4;
//array1 = array2;
//array4 = array1;
array5 = array3;
}
}
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Fri Aug 16, 2013 8:58 am
by admin
That's how the grammer of the language is designed. Anything outside the methods has to be either a declaration or a static or instance bloc. A declaration may include an initialization part, which can be a call to a method as well. So if you put something like array2 = array3; outside any method, it is neither a declaration and neither a static or instance code block. So it will not compile.
-Paul.
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Thu Feb 06, 2014 11:14 pm
by 1stTiger
The last answer can't be valid since it doesn't have a semicolon. "array5 = array3"
Re: About Question enthuware.ocajp.i.v7.2.1286 :
Posted: Fri Feb 07, 2014 3:22 am
by admin
1stTiger wrote:The last answer can't be valid since it doesn't have a semicolon. "array5 = array3"
You are right. Fixed.
thank you for your feedback!