aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes arraycopy() question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "arraycopy() question" Watch "arraycopy() question" New topic
Author

arraycopy() question

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125

My question is: why line1 and line2 with arraycopy could run well, while line3 couldn't compile.
From the API specification (quoted below), I thought at least line1 should give an ArrayStoreException.
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
if any actual component of the source array from position srcPos through srcPos+length-1 cannot be converted to the component type of the destination array by assignment conversion, an ArrayStoreException is thrown.

Please correct me!! TIA!
[ August 25, 2002: Message edited by: Yan Bai ]

SCJP 1.4
Isabel Wanderley
Ranch Hand

Joined: Aug 24, 2002
Posts: 42
I think line 3 don't compile because you are doing an assignment from a superclass to a subclass, if U put a cast like bs = (B) as;
it will work fine...
Isabel Wanderley
Ranch Hand

Joined: Aug 24, 2002
Posts: 42
Hummm... I understood now the question... I didn't understand what happened too...:/
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You aren't getting an ArrayStoreException because both arrays contain only nulls, and null is assignable to any object variable.
If you actually put some A's in your aa array, and some B's in your ba array, you should get the Exception you want.
[ August 25, 2002: Message edited by: Ron Newman ]

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
You are right. Thanks
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
But I still cannot understand why the code1 below can run well while code2 cannot.



If arraycopy() works as a for loop, and copy each component from source array to the destination array, then why the following code can give a compile error?

Please give me more details, thanks!
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Yan:
This is very interesting. I would rationalize it in following manner:
In a polymorphic statement you can declare the type of object higher in class hierarchy while actually store an object of sub-class. For example:
A as = new B()
While another object can declare and store same type. For example:
B bs = new B()
You reported that following statement fails compilation:
bs = as; //Compile error: incompatible
This is explicit assignment and declared type plays a role. The declared
type of as i.e. A is being assigned to object bs whose declared type is B. That is a down-cast from A to B, which is not allowed unless it is explicit which is not the case. Alternatively, following should compile:
bs = (B)as;
If you reproduce same situation in an array as follows:
A[] aa = new A[] { new B() } // polymorphic: declared type is A while stored type is B
B[] ba = new B[] { new B() } // non-polymorphic same declared type and stored type
ba[0] = aa[0];
The above statement will have same compilation issue because of a down-cast situation in delcared type (from A to B).
However, following should compile:
ba[0] = (B)aa[0];
The interesting point is that when you are doing same assignment using arraycopy, then delcared type is immaterial. The thing that is checked is the actual type of object. Furthermore, compiler will allow an up-cast i.e. an actual type B can be replaced with actual type A. However at run time both actual types should exactly match or you get exception. See following code:

Hope this helps.
Barkat
[ August 26, 2002: Message edited by: Barkat Mardhani ]
[ August 26, 2002: Message edited by: Barkat Mardhani ]
[ August 26, 2002: Message edited by: Barkat Mardhani ]
[ August 26, 2002: Message edited by: Barkat Mardhani ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I think I can explain this more simply:
Your first code sample is trying to copy some A's into an array of B's. Since an A is not a B, this is not allowed and causes an ArrayStoreException.
Your second code sample is copying B's into an array of B's, and that's fine.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Ron:
How would you explain that arraycopy works but for loop does not in Yan's code (in the end)? Also Yan mentioned issue of single object vs array objects copy behavoir difference in the beginning of his question.
Thanks
Barkat
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I don't see an example where a loop works but arraycopy() doesn't. If you have one, please post it.
The compiler can't check the arguments to arraycopy(),other than to ensure that they are both Object arrays. So any type checking gets deferred to runtime.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Please read Yan's two posts. You will see all the
questions I am trying to answer in my note above...
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056

gives a compiler error because you need a cast:

Once you've initialized the "aa" array, the compiler no longer remember that it contains B objects.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Hi Barkat & Ron, thanks very much for the reply. please let me simplify my question (I think Barkat knew where my question is), I apologize for the unclear statements

My question is about line7. It cannot compile and run UNLESS there's an implicit downcast (as line4) invoked when the assignment happens. But from the API document, I didn't find any explanation about this.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The src and dst arguments to System.arraycopy() are of type Object . So the compiler will let you pass any arrays of any type to this method, without checking or complaining.
Each time this method copies an array element, it does a runtime type-check to determine if this element can actually be legally stored in the dst array. If the type-check fails, you get an ArrayStoreException.
[ August 26, 2002: Message edited by: Ron Newman ]
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
That's the answer Thanks, Ron.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arraycopy() question