Home Tutorial Java Corejava Java copyOf example

 
 

Java copyOf example
Posted on: April 9, 2010 at 12:00 AM
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:

Related Tags for Java copyOf example :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.