Skip to main content

Unit 1 · Topic 1.7

1.7 Application Program Interface (API) and Libraries

Programmers rarely build everything from scratch. They use libraries of classes other people have written, and they learn how to use those classes by reading the documentation. This topic is about reading a class's documentation to find what its objects know and what they can do.

Key terms

  • library
  • API
  • package
  • class
  • attribute
  • behavior

Libraries, packages and APIs

A library is a collection of classes that are ready to use. Java comes with a huge standard library, with classes for text, math, lists, files and much more.

Classes in a library are grouped into packages, which are like folders of related classes. A few you'll meet this year:

  • java.lang: basic classes such as String, Math, Integer and Double. These are available automatically.
  • java.util: utility classes such as ArrayList and Scanner. You need an import statement to use them.
  • java.io: classes for files, such as File and IOException. These need an import too.

Import statements

An import statement at the top of the file tells Java which library class you mean:

import java.util.ArrayList; public class Roster { private ArrayList<String> names = new ArrayList<String>(); }

Without the import, the compiler doesn't know what ArrayList is and reports an error.

An API (application programming interface) specification is the documentation for a library. It lists each class's constructors and methods, what they need and what they do. The Java Quick Reference you get on the exam is a small API: it lists exactly the library methods the exam can use.

Classes, attributes and behaviors

Each class defines a new reference type. Once a class exists, you can create objects of that type, even if you never see the code inside it.

A class describes two things about its objects:

  • Attributes: the data each object stores, kept in variables. A Playlist might store its name and its songs.
  • Behaviors: what an object can do, or what you can do with it, written as methods. A Playlist might let you add a song or ask for its length.

Reading a method listing

Documentation describes each method with a header such as int getLength() or void addSong(String title). Read it left to right: the return type (what you get back, or void for nothing), the name, then the parameters in parentheses (what you must pass in, and their types).

For example, the Quick Reference lists int length() for String. That tells you length takes no arguments and gives back an int, so int n = word.length(); is a correct way to use it.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Using a class from its documentation

    A Playlist class is documented like this: a constructor Playlist(String name); void addSong(String title), which adds a song; int getLength(), which returns the number of songs; and String getName(), which returns the name. Name the attributes and behaviors, then work out what this code prints.Playlist mix = new Playlist("Road Trip"); mix.addSong("Highway Song"); mix.addSong("Open Road"); System.out.println(mix.getName() + ": " + mix.getLength());

    Show the solution
    1. Step 1: Attributes are the data a playlist keeps track of: its name and its list of songs. You can tell from what the methods return and change.
    2. Step 2: Behaviors are the methods: addSong, getLength and getName.
    3. Step 3: Line 1 creates a playlist named Road Trip. Lines 2 and 3 add two songs.
    4. Step 4: Line 4 calls getName(), which returns "Road Trip", and getLength(), which returns 2. They're joined with a colon and a space.
    5. Step 5: You never saw how Playlist stores its songs, and you didn't need to. The documentation was enough.

    Answer: Attributes: the name and the songs. Behaviors: addSong, getLength and getName. The code prints Road Trip: 2.

Common mistakes

  • Mixing up attributes and behaviors. Attributes are data (nouns, stored in variables); behaviors are actions (verbs, written as methods).
  • Forgetting the import for ArrayList, Scanner or File. Classes in java.lang, like String and Math, don't need one.
  • Ignoring the return type in documentation. A void method gives nothing back, so you can't store its result in a variable.

On the exam

  • Free-response questions describe the classes you need in the same style as API documentation. Read every method header carefully and call methods exactly as documented, with the right number and types of arguments.
  • Keep the Java Quick Reference open during the exam. If a method isn't on it or in the question, you probably don't need it.

Connected topics

Videos

  • AP Computer Science A - Topic 1.7: Application Programming Interface (API) and Libraries

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Application Program Interface and Libraries

    CodeHSWatch on YouTube (opens in a new tab)

  • Get an overview of the Java API documentation and how to use it

    IBM DeveloperWatch on YouTube (opens in a new tab)

  • Navigating an API

    CodeHSWatch on YouTube (opens in a new tab)

  • Imports & Packages | Java | Tutorial 38

    Giraffe AcademyWatch on YouTube (opens in a new tab)

Check yourself

3 questions on 1.7 Application Program Interface (API) and Libraries. Pick an answer to see if you got it, and why.

Question 1 of 3

A programmer is designing a PlaylistSong class for a music app. Which of the following is best modeled as a behavior (a method) rather than an attribute?

Question 2 of 3

A student wants to use a class from a Java library that she didn't write. Which of the following is the main thing its API documentation tells her?

Question 3 of 3

Which of the following statements about Java libraries is true?

0 of 3 answered