Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.197 :

Posted: Wed Jul 18, 2012 8:20 am
by Diego Almeida
Question:

What will be the output of compiling and running the following program?
class CloneTest
{
public static void main(String[] args)
{
int ia[ ][ ] = { { 1 , 2}, null };
int ja[ ][ ] = (int[ ] [ ])ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
}
}


Answer:
It will print 'false true' when run.


Explanation:
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared, so ia and ja are different but ia[0] and ja[0] are same.

why? how prints false in the first operation? The Explanation said: "is[0] and ja[0] are same!" someone help me?

Re: About Question com.enthuware.ets.scjp.v6.2.197 :

Posted: Wed Jul 18, 2012 8:07 pm
by admin
false is printed because of

Code: Select all

System.out.print((ia == ja) + " ");
true is printed because of

Code: Select all

System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);


See the attached image:
arrayclone.gif
arrayclone.gif (2.45 KiB) Viewed 3987 times
You can see that ia and ja point to different arrays so ia == ja is false. ia[0] == ja[0] as well as ia[1] == ja[1] are both true. So it prints true.

HTH,
Paul

Re: About Question com.enthuware.ets.scjp.v6.2.197 :

Posted: Thu Jul 19, 2012 7:09 am
by Diego Almeida
Tks! I'd not seen the "print" without "ln", I had thinking the first sysout don't print nothing. Understand thoroughly!