lbzl
lbzl

Reputation: 67

Why does java use Object instead of T[] in method declaration when Object actually refers to an array

I was reading at the Oracle java API document about System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) method. ArrayStoreException is thrown when the src/dest argument refers to an object that is not an array. Then is it possible to to use Arrays or T[] in the method declaration? Something like:

System.arraycopy(Arrays src, int srcPos, Arrays dest, int destPos, int length)

or

System.arraycopy(T[] src, int srcPos, T[] dest, int destPos, int length)

Upvotes: 1

Views: 76

Answers (1)

newacct
newacct

Reputation: 122429

System.arraycopy() can be used on both arrays of primitives, and arrays of references. T[] can only be used with arrays of references -- T represents a reference type. The only supertype of both array-of-primitives types and array-of-references types is Object (well, there are some interfaces like Cloneable and Serializable that are also supertypes for all array types but they are not appropriate).

Upvotes: 1

Related Questions