The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes System.arraycopy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "System.arraycopy" Watch "System.arraycopy" New topic



Joined: Mar 15, 2001
Posts: 5
In Java you can copy an array into another using the normal assignment operator =
public class ArrayTest {
public static void main (String[] args) {
Object[] ov;
String[] sa = {"Green", "Blue", "Red"};
ov = sa;
System.out.println("Color = "+ov[1]);
So why and when should I use the System.arraycopy ?
Moreover, why in Java ov = ... is not the same as ov[0]= like in C++?
Naveen Sharma
Ranch Hand

Joined: Mar 23, 2001
Posts: 65
Hi Mirko
Here What r u doing is using the same object but with diffrent refrence variable ov,sa. Basically this is a wide conversioning.You r storing an String object refrense in an Object type variable. You have not copy the contents. but you r using the contents by two diffrent refrence names
But arrayCopy function basically copies the contents of two objects(array), from source to destination.
In former case you r taking the same object but two refrences pointing to the same object
but later is for two objects.
Hope u can understand Now

Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
So why and when should I use the System.arraycopy?

public static void arraycopy(Object�src,

You can see that System.arraycopy has arguments for specifying the position in the array to copy from/to and the length of the copy. Using the normal assignment operator, that would have to be done by looping through the arrays. It is just a matter of writing less code when you only want to copy part of an array.
I agree. Here's the link:
subject: System.arraycopy
It's not a secret anymore!