I was going through some basic MCQ questions in Java and I was unable to understand this one.
public class CommandArgsThree {
public static void main(String[] args) {
String[][] argCopy = new String[2][2];
int x;
argCopy[0] = args;
x = argCopy[0].length;
for (int y = 0; y < x; y++) {
System.out.print(" " + argCopy[0][y]);
}
}
}
and the command-line invocation is
java CommandArgsThree 1 2 3
Now what I can't understand is that the argCopy
has been declared as a 2D array then how can it be used as 1D couple of lines later where argCopy[0]
has been assigned the value of args?
P.S: I also know that argCopy[0] is 1D array that's why I am asking how can we use the 2D array as 1D here?Means is it legal to do so?