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:

 

Java
package sample;

public class Demo {
  public static void main(String[] args) {
    //src array of char type
    char[] copyFrom = {'d', 'e', 'd', 'u', 't', 'e', 'c', 'h', 'l', 'e', 'a', 'r', 'n','e','r','s','z'};

    //destination array of same char type as src array
    char[] copyTo = new char[15];

    //calling of arraycopy() function with srcPos = 1, destPos = 0 and length = 15 
    System.arraycopy(copyFrom, 1, copyTo, 0, 15); //First call
    System.out.println(new String(copyTo));

    //src array
   byte src[] = {65,66,67,68,69,70};
   
  //destination array
   byte dest[] = {71,72,73,74,75,76};

    //copy the src array from position 2 i.e. 67 to next three index into dest array index 1 i.e. 72 
    System.arraycopy(src, 2, dest, 1, src.length - 3); //Second Call
    System.out.println(new String(src) + " " + new String(dest));
  }
}
Java

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 which 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 the src array name.
    4. destPos, the starting position/index in the destination array from which the elements need to be replaced, and
    5. length, the number of array elements to be copied into 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:

edutechlearners
ABCDEF GCDEKL

Description:

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.

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 contribute@edutechlearners.com (The contents will be published by your Name).

Leave a Reply