Short on time? This page is the recap. For explanations and worked examples, use the full study guide.
Recap
- A library is a collection of classes. An application programming interface (API) specification informs the programmer how to use them.
- Classes in the APIs and libraries are grouped into packages.
- A class defines a specific reference type. Existing classes and class libraries can be used to create objects.
- Attributes are the data related to the class, stored in variables.
- Behaviors are what instances of the class can do (or what can be done with them), defined by methods.
- The documentation in API specifications is essential for understanding the attributes and behaviors of a class.
- The Java Quick Reference, provided on the exam, lists accessible library methods and explains them.
Key relationships
| Idea | Example |
|---|---|
| Default package | java.lang: String, Math, Integer |
| Needs an import | java.util: Scanner, ArrayList |
| Method entry | static double sqrt(double x): return type double, name sqrt, one double parameter |
| Return type position | before the method name |
| Parameter list | after the name, in brackets, with types in order |
String attribute |
the sequence of characters |
String behaviors |
length(), substring(...), indexOf(...), equals(...) |
indexOf not found |
returns -1 |
substring(from, to) |
characters from index from up to but not including to |
int passed to a double parameter |
converted automatically |
Assumptions and conventions
- You are expected to read documentation, not to memorise it.
- A method entry gives return type, name and parameters, plus a description of what it does.
- A class method such as
Math.sqrtis called with the class name and a dot (Topic 1.10). - A missing import produces a compile-time “cannot find symbol” error.
A four-step routine for any API entry
- Decide how the method is reached: through an object, or through the class name (a
staticmethod). - Match the parameters: the number, the types (an
intfits adouble; the reverse needs a cast) and the order. - Use the return type: the call is an expression of that type, and
voidmeans there is no value to use. - Read the description for special cases, such as “returns -1 if not found” or “ending at index
to - 1”.
Mistakes to avoid
- Confusing the library with its documentation.
- Expecting every class to be available without an import.
- Mixing up attributes (data) and behaviors (methods).
- Ignoring the return type, for example storing a
doubleresult in anintvariable. - Passing arguments in the wrong order or number.
- Forgetting that
substring(from, to)excludes indexto. - Forgetting the special return value
-1for “not found”.
Quick self-check
- Which package holds
StringandMath? (java.lang, available by default.) - In
String substring(int from, int to)what is the return type? (String) - Is
length()an attribute or a behavior ofString? (A behavior: it is a method.) - What does
"banana".indexOf("x")return? (-1) - What does
Math.pow(2, 3)return and of what type? (8.0, adouble)
Next: practice questions.