Share on Google+Share on Google+

Java copyOf example

Advertisement
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:

Advertisements

Posted on: April 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.