by Amanda Moreton, with updates by the staff of CS 1331
API stands for Application Programming Interface. It holds information for all of the classes that you get with the Java Development Environment. It’s awesome! Other people have done some work that we can use to make our own work easier. The API can be a super useful tool if you know how to use it. Let’s figure out how to take advantage of all the API has to offer.
Top left: a list of all of the packages you have access to (Think of packages like folders in your own computer. The packages contain the classes in the API)
Bottom left: a list of all the classes we can look through and use. There are a bunch, so use CONTROL + F to search for a specific class quickly. Interfaces are in italics.
“Frames”/”No frames” at the top refers to the packages and classes panels on the left. If you cannot see the panels to search through, you can click “FRAMES” to add them to your screen
BLUE ARROW: The name of the class is at the top in bold
YELLOW ARROW: Underneath, we see the package where this class is held. We need to import the bottom line of the hierarchy (in this case, java.util.Random) in order to be able to make an object of this class. We do not need to import if the class is in java.lang.
RED & GREEN ARROWS: Java tells us which interfaces this class implements, if any, and what classes are subclasses of this class, if any.
FIRST CHUNK OF TEXT: Sometimes, this intro text holds a code example with an object of the given class. Unfortunately, not this time :(
Scroll further down on the page to find…
Let’s check out the Collections class’s method summary for an example of static methods
Collections.methodName()
Now, let’s practice!
Let’s try to change a String to be uppercase and replace all “A”s with “X”s.
Nope! The String
class is in the java.lang
package, so we have access to it automatically. This makes sense, since we can create a new String
whenever we want, as we’ve already seen!
toUpperCase()
looks good! It takes in no parameters and converts all of the characters in the String to upper case. We have to call this method on a String because the toUpperCase()
method is an instance method.
We would know if it was a static method because it would say static in front of the return type, like it does for the valueOf methods. It’s important to note that this method returns a String. It is not changing our pre-existing String. So, if we have the String:
String stringToChange = “Today is a beautiful day!”;
We can save the upper case version in a new variable:
String changedString = stringToChange.toUpperCase();
If we print out changedString, it will read:
“TODAY IS A BEAUTIFUL DAY!”
Perfect! This method works similarly to the toUpperCase()
method, but takes in parameters. All we have to do is specify that we want to replace ‘A’, our old character, with ‘X’, our new character.
This time, instead of using a new variable, let’s reassign our changedString variable:
changedString = changedString.replace(‘A’, ‘X’);
Our new string would print out:
“TODXY IS X BEXUTIFUL DXY!”