System.ArrayCopy Method in Java with Example

Today we will discuss about the topic: System.ArrayCopy Method in Java with Example from our Java experts. The arraycopy( ) method is present in java.lang.System package. The arraycopy() method is mainly used to copy quickly an array of any type from one place to another. This is much better and faster approach than the equivalent loop in Java. The following is the declaration of arraycopy( ) method in java.lang.System:

The above declaration demonstrates the following points:

  • The arraycopy( ) method is a native method i.e. code that does not run in the JVM. It’s typically written in C or C++. (Native methods are usually used to interface with system calls or libraries written in other programming languages). Being a native method makes it faster than any other Java method as C/C++ is much faster than Java.
  • It is the static method i.e. can be called simply with the help of class name (i.e. System).
  • It accepts five different arguments:
    1. src, the source array name of any type from where the array elements need to be copied.
    2. srcPos,  the starting index/position in the source array.
    3. dest, the destination array name or the resultant array name of the same type as src array name.
    4. destPos, the starting position/index in the destination array from which index the elements need to be replaced, and
    5. length the number of array elements to be copied in the resultant/destination array.
  • It is of void return type i.e. does not return any value.
  • Also, the method throws three different types of exceptions:
    1. IndexOutOfBoundsException: if copying would cause access of data outside array bounds.
    2. ArrayStoreException: if an element in the src array could not be stored into the destination array because of a type mismatch so the data type of src and destination should be same.
    3. NullPointerException: if either src or destination array is null.

The output of the above code is:



In the first call of arraycopy( ) method, the elements from copyFrom array (from index 1 to 15) are copied to the new array copyTo.

In the second call of arraycopy( ) method, the src and dest array is declared as byte and the src array (from index 2 to the src array length-3 i.e. 3) are copied to dest array (from index 1 to next three elements).

That’s it for now. Hope you understood the concepts of System.ArrayCopy Method in Java now.

Print Friendly, PDF & Email

Read more on Computer Programming articles:-

 Computer Programming Articles

Contribute to EduTechLearners:-

Please write comments if you want to share more information regarding the above notes. If you want some more notes/books on any of the topics please mail to us or comment below. We will provide you as soon as possible and if you want your’s notes to be published on our site then feel free to contribute on EduTechLearners or email your content to (The contents will be published by your Name).

Leave a Reply