Java copyOf example


 

Java copyOf example

This tutorial demonstrate the copyOf method. In the below listed program you will see how copyOf method is help in copy one array variable's values into a new array variable.

This tutorial demonstrate the copyOf method. In the below listed program you will see how copyOf method is help in copy one array variable's values into a new array variable.

Description:

The copyOf method copies the specified array, truncating or padding with zeros so that the copy has the specified length. The result of this method allow to have the same of content in newly copied array.

Code:

import java.util.Arrays;

public class CopyOfExample {
  public static void main(String args[]) {
    String str[] "a""b""c" };
    String copy[] = Arrays.copyOf(str, 3);
    System.out.printf("value of str \t\t%s\n", Arrays.toString(str));
    System.out.printf("Value of copy\t\t%s", Arrays.toString(copy));
  }
}

Output:

Ads