API SelectionThe Application Programming Interface (API) which comes with Java is a standard library of classes across all platforms, the only differences are between the different versions of Java, 1.0,1.16,1.18,1.2,1.22,1.3 etc, and the different editions, Java Standard Edition, Java Micro-Edition Java 2 Enterprise Edition etc. You don't need to worry about that for now though. Basically the rule here is that with a lot of the classes in the api they make use of JNI calls to faster native implementations. For example, rather than use the following piece of code to copy an array:
Use the following API call instead:
This makes use of some native code and is much, much faster. Wherever appropriate make use of API calls. Bear in mind, however, that use of classes such as the Collection classes which take a generic Object, can prove to be a lot slower than if you used a class of your own specific to the type of Object you are using, as you are otherwise hit with a lot of casts. An extreme example would be if you where to try and store ints in a Vector. As an int is a primitive, you also have to convert it to an Integer to add it to the Vector:
When it comes to retrieving that int you have an even worse situation, you have to cast the returned Object to an Integer and then call the intValue() method on it:
A bit of an extreme example I'll agree, but it shows a situation in which the use of a custom class could speed things up considerably.
|
|||||||||||