Java System arraycopy() Method

The arraycopy() method of Java System class returns or copies a subsequence components of a specified source array, begins at a specified position(referred as"srcPos") of source array(referred as "src") to the specified position(referred as "destPos") of destination array (referred as "dest"). The "length" argument is for number of components to be copied.

Syntax

Parameters

src - Source array (Object type)

srcPos - Starting position in Source array (Integer type)

dest - Destination array (Object Type)

destpos - Starting position in destination array (Integer type)

length - Number of elements to be copied (Integer type)

Returns

  1. If dest or src is null then a NullPointerException is thrown.
  2. If src and dest arguments refers same array object then copying of components will begin with srcPos. First the components will be copied a temporary array and then to position of destPos of destination array.
  3. If any of int type argument have negative value then an IndexOutOfBoundsException is thrown.
  4. If src and dest arguments refers to an object that is not an array then ArrayStoreException is thrown

Example 1

Output:

Source array:1234321
Destination array:5678765
Source Position:1
Destination Position:2
Length:4
Destination array after use of arraycopy()
5623435

Example 2

Output:

Source array:ABBHAM
Destination array:SHUNUL
Source Position:2
Destination Position:3
Length:4
Destination array after use of arraycopy()
SHUBHAMJADON