Java System Class Implementation Guide

designjava

The Java System class contains various data members and methods that make perfect sense being there. For instance:

System.in (variable)
System.err (variable)
System.out (variable)
System.exit(int)
System.gc()
System.getSecurityManager()

etc. However, there is one method that I don't understand being there:

System.arraycopy(Object, int, Object, int int)

Copying one array to another feels to me like it belongs in the Arrays class; following from the documentation:

This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

Methods for manipulating arrays is what points me to this conclusion, as copying one array to another is surely array manipulation, right?

So my question: why is arraycopy() in System?

Is it a relic of an early Java System class implementation? The method isn't marked as deprecated, so I'm a little bit lost. Furthermore, it doesn't follow the Java camelCase standard, which brings me back to my thinking that it's a relic of the early library design.

Best Answer

System.arraycopy is implemented natively by each JVM. Here is the method declaration:

public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

This means that it does array copying in the fastest way possible in native assembly instructions. The idea is that this is such a commonly needed, potentially slow functionality that Java should provide this behavior at a low level.

Other ways of copying arrays either wrap System.arraycopy, or use interpreted loops, which would not be as performant.