8

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.

  • Probably because there used to not be a good place to put it, and now it's too late to move it. – immibis Mar 19 '15 at 9:23
8

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.

  • Okay this makes a bit of sense now; I didn't know that each JVM had its own implementation of this. That said, are all native methods in System? By naming convention it kind of makes sense, because they are system-dependent, but from an organizational standpoint, arraycopy would make sense to be defined in the Array class or something similar, and have its native implementation there. Personally I've used arraycopy only once in my 5 years of programming. – Chris Cirefice Mar 17 '15 at 19:44
  • 5
    It's still there for backwards compatibility reasons. Also, while not all native methods are in System, there are no native methods in java.util.Arrays. – durron597 Mar 17 '15 at 19:50
  • I see, well that clears up the questions that I had, thanks! – Chris Cirefice Mar 17 '15 at 19:53
4

Is it a relic of an early Java System class implementation?

System.arraycopy is one of the oldest parts of the library, while java.util.Arrays was added in 1.2. I wouldn't quite call arraycopy a relic as there isn't anything that directly replaces it, and the various copy methods in Arrays are implemented in terms of it.

There's nothing special about System or Arrays that requires native methods to be in one or prevents them from being in the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.