The Stacks
UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.7

1.7 Application Program Interface (API) and Libraries

Java ships with thousands of classes. An API tells you what they do and how to call them, without showing the code inside.

What you need to know

  • A library is a collection of classes written and tested by others. Java's standard library includes String, Math, Scanner, ArrayList, and more.
  • An API (Application Program Interface) is the documentation of a library: each class, its methods, their parameters, return types, and what they do. You program to the API without knowing the implementation.
  • The AP exam provides a Java Quick Reference — a mini-API listing exactly the methods you may use from String, Math, Integer, Double, ArrayList, and Object.
  • Classes in the java.lang package (String, Math, Integer, Double) are available automatically. Others (Scanner, ArrayList, File) require an import statement at the top of the file.
  • Reading documentation is a skill: the method header tells you the return type, name, and parameter types. That's enough to call it correctly.
  • Using a library is abstraction in practice — you rely on what a method does, not how.

Worked example

import java.util.Scanner;    // needed: not in java.lang
import java.util.ArrayList;

public class Demo
{
    public static void main(String[] args)
    {
        String s = "abc";                    // String: no import
        double r = Math.sqrt(16);            // Math: no import
        ArrayList<String> list = new ArrayList<String>();
    }
}
Exam tip: If a question asks why a program won't compile and it uses Scanner or ArrayList without an import, that's the answer. If it asks what a method returns, look at the header's return type — the exam sometimes gives you a header for a made-up method and expects you to use it correctly from the header alone.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • The Java Quick Reference provided on the exam lists exactly: String (length, substring ×2, indexOf, equals, compareTo), Integer (MIN_VALUE, MAX_VALUE, parseInt), Double (parseDouble), Math (abs, pow, sqrt, random), ArrayList (size, add ×2, get, set, remove), Object (equals, toString), File and Scanner (the input methods). Anything not listed you should treat as unavailable on FRQs.
  • Reading a method in an API: public int indexOf(String str) tells you it's public (callable), returns an int, is named indexOf, takes one String. That's everything needed to call it correctly.
  • java.lang is imported automatically: String, Math, Integer, Double, Object, System. Everything else needs import package.Class; at the top of the file, before the class declaration. java.util.Scanner, java.util.ArrayList, java.io.File, java.io.FileNotFoundException are the ones on the exam.
  • Libraries are abstraction: you don't know how Math.sqrt computes a square root and don't need to. The API is the contract. This is the same idea as procedural abstraction in 3.1.
  • On FRQs, the problem often gives you a class with its method headers and says "you may use these." Treat that as an API: use the methods as specified, don't reimplement them, and don't assume methods that aren't listed.

Mistakes that cost points

  • Using methods not on the reference sheet. charAt, contains, split, toCharArray, Math.round, Math.floor — none are on it. On an FRQ they may not earn credit even if they'd work in real Java.
  • Forgetting an import. Using Scanner or ArrayList without importing is a compile error and a standard question.
  • Assuming a method's behavior instead of reading it. If an FRQ defines getScore(), read what it returns. Don't guess.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 Which of the following best describes an API?
  1. A The source code of a library
  2. B The documentation describing a library's classes, methods, parameters, and return values
  3. C A type of run-time error
  4. D A Java keyword
Show answer

Answer: B. An API is the interface specification — what you can call and how — not the implementation.

Q2 Which of the following classes requires an import statement before it can be used?
  1. A String
  2. B Math
  3. C Scanner
  4. D Integer
Show answer

Answer: C. Scanner is in java.util. The other three are in java.lang, which is imported automatically.

Key vocabulary

Library
a collection of pre-written classes available for use
API
documentation describing how to use a library's classes and methods
import
a statement that makes a class outside java.lang available in a file
Java Quick Reference
the list of methods provided on the AP exam