AP Computer Science A · Study guide

APIs and Libraries

Unit 1 · Using Objects and Methods · Topic 1.7

APIs and Libraries

Almost every class you will ever use was written by someone else. This is how you find out what it does without reading its source.

Exam-day recap
  • A package is how classes in APIs and libraries are grouped.
  • A library is a collection of classes.
  • An API specification is the documentation that informs the programmer how to use those classes.
  • A class defines a specific reference type and can be used to create objects.
  • The library is the code; the API specification is the description of it — they are not the same thing.
  • Attributes are the data related to the class, stored in variables; behaviors are what instances can do, defined by methods.
  • If it holds a value it is an attribute; if it does something, even just returning a value, it is a behavior.
  • A reference variable holds a reference, never the object itself, and can hold null before it refers to anything.
  • An API specification tells you what you may depend on and withholds the rest, so the class can change everything else without breaking you.
  • Facing an unfamiliar class, get four answers from its documentation: what it stores, what it does, how to construct it, how it fails.

1The Skill Being Taught

You will use classes in this course that you did not write and cannot see inside. You still have to know exactly what they do. The mechanism that makes that possible is documentation, and reading it is the skill.

The learning objective

1.7.A — identify the attributes and behaviors of a class found in the libraries contained in an API. Not "write a library", not "memorize one". Identify, from documentation, what a class stores and what it can do.

2Four Words, Nested

The vocabulary here is small and precise, and questions test the distinctions:

TermWhat it is
PackageHow classes in APIs and libraries are grouped
LibraryA collection of classes
API specificationThe documentation that informs the programmer how to use those classes
ClassDefines a specific reference type — and can be used to create objects

Two of those four words name code and one names paper. Keeping them apart is most of the topic:

The relationship, in one sentence

Classes are grouped into packages; a collection of classes is a library; the API specification is the document that tells you how to use them. The library is the code. The API specification is the description of it. They are not the same thing, and a question that treats them as synonyms is testing whether you noticed.

2.1 "A Class Defines a Specific Reference Type"

That phrase from the essential knowledge is doing real work. When a library gives you a class, it is giving you a type you can declare variables of:

String name;        // String is a class, so String is a type
Random generator;  // so is Random

Both are reference types, which is why a variable of that type holds a reference to an object rather than the object itself, and why it can hold null before it refers to anything. Every class in every library you import extends the set of types available to you.

What that buys you is visible the moment you declare one:

3Attributes and Behaviors

The course splits everything a class offers into exactly two categories:

AttributesBehaviors
AreThe data related to the classWhat instances can do, or what can be done with them
Stored/defined inVariablesMethods
Answer"What does it know?""What can it do?"
A reliable test

If it holds a value, it is an attribute. If it does something — even if that something is just handing a value back — it is a behavior. A method that returns the length of a string is a behavior, not an attribute, because it is defined by a method.

4Reading an API Entry Cold

Here is documentation for a class you have never seen. You are not expected to recognize it — that is the point.

class TicketCounter — package venue.boxoffice

A counter that issues sequentially numbered tickets for a single event.

MemberDescription
TicketCounter(String eventName, int capacity)Creates a counter for the named event that will issue at most capacity tickets.
String nextTicket()Issues and returns the next ticket code. Returns null if the event is sold out.
int remaining()Returns the number of tickets not yet issued.
boolean isSoldOut()Returns true if no tickets remain.
String getEventName()Returns the name of the event.

Without a single line of its source, that entry answers everything you need:

QuestionAnswer, and where it came from
What package is it in?venue.boxoffice — stated in the heading
What are its attributes?An event name and a capacity — inferred from the constructor and the accessors that return them
What are its behaviors?Issue a ticket, report how many remain, report sold-out status, report the event name — the four methods
How do I make one?new TicketCounter("Spring Concert", 250)
What happens when it runs out?nextTicket() returns null — stated explicitly, and easy to miss

Annotated, the entry stops being a list of methods and becomes a set of answers:

The line that ruins programs

"Returns null if the event is sold out." Recall what null is from the start of this guide: a reference that points at no object at all. On a sold-out event, nextTicket() has no ticket code to hand back — so it hands back nothing, in the most literal sense Java allows.

Here is the crash, frame by frame:

String code = counter.nextTicket();   // sold out — code holds null
int len = code.length();              // asks a method OF a null reference

Line one compiles and runs. Line two asks a null reference to do something — and Java halts the program on the spot. Neither line is wrong in isolation; the disaster is the combination of a documented empty return and a caller that never checked. That is what "ruins programs" means here: not a wrong answer, a stopped program.

The guard is one comparison, and the documentation just handed you both operands:

String code = counter.nextTicket();
if (code != null)
{
    System.out.println(code);
}
else
{
    System.out.println("sold out");
}

That behavior is documented, so a program that breaks on it is a program whose author did not finish reading — which is exactly what this topic is about.

4.1 Using It Without Seeing It

TicketCounter counter = new TicketCounter("Spring Concert", 250);

while (!counter.isSoldOut())
{
   String code = counter.nextTicket();
   System.out.println(code);
}

Every decision in those five lines came from the documentation. The loop guard uses isSoldOut() rather than counting to 250, because the documentation says that method exists and what it means. The null case cannot occur here, because the guard rules it out — a conclusion drawn from two documented behaviors together.

What you never needed

You never learned how ticket codes are generated, whether they are stored in an array or a list, or what the class does internally at all. That is the entire value of an API specification: it tells you what you may rely on, and deliberately does not tell you the rest.

4.2 What Documentation Does Not Tell You

Reading an entry well also means noticing its silences. The entry above never says whether ticket codes are unique across events, what happens if capacity is negative, or whether the counter can be reset. Those are not answered, so they cannot be relied on — and an exam question that asks "which of these can you conclude" is testing exactly that boundary.

5Why Reuse Is the Default

The essential knowledge notes that existing classes and class libraries can be utilized to create objects. That is a statement about how programs are actually built:

ReasonWhat it buys
It already worksA widely used library has been exercised by far more programs than yours ever will
It is documentedThe API specification is the contract, and it outlives whoever wrote the code
It is someone else's problem to maintainBugs get fixed without you doing the fixing
It is shorterThe code you do not write cannot be the code that is wrong

6Check Yourself

Is remaining() an attribute or a behavior of TicketCounter?

Response

A behavior. It is defined by a method, and behaviors are what instances can do. The number of remaining tickets is data the object tracks, so the underlying count is an attribute — but remaining() itself is the method that reports it. Defined by a method means behavior, every time.

What is the difference between a library and an API specification?

Response

A library is the collection of classes — the code itself. The API specification is the documentation that informs a programmer how to use those classes. One is the thing; the other describes the thing. You run the library; you read the specification.

Given only the entry above, can you conclude that two TicketCounter objects for different events will never produce the same ticket code?

Response

No. The documentation says codes are sequential within a counter and says nothing about uniqueness across counters. It is not stated, so it cannot be relied on. Reading an API well includes noticing what it declines to promise.

Why does having a class mean you also have a new type?

Response

Because a class defines a specific reference type. Once TicketCounter exists you can declare variables of that type, pass them as parameters, and return them from methods — and those variables hold references to objects rather than the objects themselves.

7Summary

IdeaWhat to remember
LibraryA collection of classes
API specificationDocumentation telling a programmer how to use those classes
PackageHow classes in APIs and libraries are grouped
ClassDefines a specific reference type; used to create objects
AttributesThe data related to the class, stored in variables
BehaviorsWhat instances can do, defined by methods
The skillIdentify both from documentation, without the source
Silences countWhat the specification does not promise, you cannot assume
The habit

Given an unfamiliar class, answer four questions from its documentation before writing anything: what does it store, what can it do, how do I construct one, and what does it do when things go wrong? The fourth is the one people skip, and it is where the null was hiding.