Java Objects, Classes, and the String API
A focused guide to Java classes, object references, constructors, methods, strings, wrapper classes, and API usage for AP Computer Science A.
Classes and Objects
Java organizes data and behavior through classes and objects. A class is a blueprint that describes a type, while an object is an instance of that class created during program execution.
A class can define:
Instance variables that represent an object’s state.
Constructors that initialize new objects.
Methods that describe what objects can do.
Class variables and methods marked
static, which belong to the class rather than to one particular object.
For example, String is a class, and `
References, null, and
A variable whose type is a class usually stores a reference to an object. The variable is not the object itself. Assigning one reference to another copies the reference, so both variables can point to the same object.
A may also contain null, meaning that it currently refers to no object. Calling an instance through null causes a NullPointerException.
The new operator creates an object and produces a reference to it. A declaration alone does not create an object; an object must be created with new, obtained from another reference, or returned by a .
When two variables refer to one object, they are aliases. This matters most for mutable objects. For example, if first and second both refer to one StringBuilder, appending through second changes the object observed through first as well. In contrast, String objects are , so string operations do not alter the existing string.
Takeaway: Always distinguish the variable, the reference it stores, and the object to which that reference points.
Constructors and Methods
A initializes an object when the program evaluates a new expression. Its name must match the class name, and it has no return type. Constructors can receive parameters and can be overloaded when their parameter lists differ.
For example, a class can provide a no-parameter for a default rectangle, a one-parameter for a square, and a two-parameter for a rectangle with separate dimensions. Java chooses the whose parameter types and number of arguments match the call.
If a class declares no , Java supplies a no-parameter . Once the class declares any , Java no longer supplies an additional no-parameter automatically.
A is a named group of statements that performs an operation. Its declaration specifies an access modifier, return type, name, and parameter list. A void performs an action without returning a value; a non-void must return a compatible value.
An instance is called on an object with the dot operator, as in word.length(). A static belongs to the class and is called through the class name, as in Math.sqrt(81.0).
Takeaway: Read a declaration carefully: a initializes an object, while a performs an operation after the object or class is available.
Working with Strings
String represents a sequence of characters, and string indices begin at 0. For a string with a given length, the last valid index is one less than that length.
Important operations include:
length()returns the number of characters.charAt(index)returns the character at an index.indexOf(text)returns the first matching index, or-1when no match exists.substring(from, to)includes the starting index and excludes the ending index.substring(from)returns the characters from the starting index through the end.contains(text)tests whether a sequence occurs.toUpperCase()andtoLowerCase()return converted strings.
For example, `
String Immutability and Comparison
Because String is , a call does not alter the original string. If word refers to `
The and Wrapper Classes
The provides standard classes and interfaces, including String, Integer, Math, and Scanner. API documentation should be used to determine how a class or works before writing a call.
When consulting documentation, identify:
The class and its package.
Whether the operation is static or an instance .
The parameter types and number of parameters.
The return type.
Any restrictions or exceptions.
A signature such as int indexOf(String str) indicates an instance of String that accepts one String parameter and returns an int. Overloaded methods share a name but have different parameter lists, so the arguments determine which version is selected.
Classes outside java.lang commonly require an import. For example, a program normally imports ArrayList from java.util before using it.
Wrapper classes represent primitive values as objects. Common pairings include int with Integer, double with Double, boolean with Boolean, and char with Character.
converts a primitive to its wrapper automatically, while unboxing converts a wrapper back to a primitive. For example, assigning an int to an Integer uses , and assigning an Integer to an int uses unboxing. A wrapper reference can be null, but a primitive cannot; attempting to unbox a null wrapper causes a NullPointerException.
Useful wrapper operations include Integer.parseInt, which returns a primitive int, Double.parseDouble, which returns a primitive double, and Integer.valueOf, which returns an Integer object. Constants such as Integer.MAX_VALUE describe limits of the corresponding primitive type.
Takeaway: API documentation connects a desired operation to the correct class, call style, arguments, return type, and possible errors.
Tracing Objects and Avoiding Errors
Object tracing requires tracking both each variable and the object or value it currently represents. For every assignment or call, ask:
What does each variable refer to now?
Do multiple variables alias the same object?
Is the object mutable or ?
Is the call an instance or a static ?
Does the call return a new object, a primitive value, or
void?
Consider the sequence String first = "AP"; String second = first; second += " CSA";. Initially, both variables refer to the string `
A Reliable Problem-Solving Checklist
The most reliable way to prevent object-related errors is to match the operation to the concept involved.
Save the result of an string when the updated string is needed.
Use
to compare string contents instead of comparing object references with==.Check that a reference is not
nullbefore calling an instance .Remember that assignment of a reference does not copy the object.
Distinguish a , which has no return type, from a , which does have a return type declaration.
Check index boundaries before using
charAtorsubstring.Distinguish
parseInt, which returns a primitive, fromvalueOf, which returns a wrapper object.
These checks connect Java’s syntax to its object model: variables hold references, methods operate according to their class, and objects produce replacement values rather than changing in place.
Final takeaway: To trace or write Java object code correctly, identify the class, follow each reference, determine whether the object can change, and verify the exact or signature.