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?
About Question com.enthuware.ets.scjp.v6.2.197 :
Moderator: admin
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.197 :
false is printed because of
true is printed because of
See the attached image: 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
Code: Select all
System.out.print((ia == ja) + " ");
Code: Select all
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
See the attached image: 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 :
Tks! I'd not seen the "print" without "ln", I had thinking the first sysout don't print nothing. Understand thoroughly!
Who is online
Users browsing this forum: witek_m and 11 guests