Understanding the API

by Amanda Moreton, with updates by the staff of CS 1331

What is the API?

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.

Parts of the API

The Homepage

A Class Page

Scroll further down on the page to find…

Constructor and Method Info

Let’s check out the Collections class’s method summary for an example of static methods

Now, let’s practice!

Let’s try to change a String to be uppercase and replace all “A”s with “X”s.

1. Let’s find the String class on the homepage

2. In order to use all of the methods in the String class, do we need to import anything?

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!

3. Let’s scroll down to look for a method to make our String uppercase

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:


4. Next, let’s see if there’s a method to help us replace all of our “A”s with “X”s

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: