AP Computer Science A

Using objects and methods

Use Java values, objects, and methods

Read types, expressions, method calls, and String operations in Java.

Examples use Java. Read and trace them here; run them in a Java environment. The studio executes pseudocode.

Follow the type of each expression

Java distinguishes primitive values such as int, double, and boolean from object references such as String. Integer division discards the fractional part: 7 / 2 is 3. Converting an operand to double before dividing gives 3.5; converting the already-computed integer result does not restore the fraction.

A method call has a name, arguments, and possibly a return value. String positions start at 0. substring(0, 3) includes position 0 and excludes position 3. Strings are immutable, so taking a substring does not change the original object.

public class Demo {
    public static void main(String[] args) {
        int total = 7;
        double mean = (double) total / 2;
        String name = "Computer";
        System.out.println(mean);
        System.out.println(name.substring(0, 3));
    }
}

Compare content deliberately

For primitive numeric values, == compares values. For object references, == asks whether the references identify the same object. Use a String's equals method when the question is whether its character content matches another string.

Read an API method's signature and preconditions before using it. For substring, check the bounds against length(). A program that compiles may still fail at runtime when an index is outside the allowed range.

Check yourself

What two lines does the example print?Show answer

3.5 and Com. The cast happens before division, and substring excludes its end index.

References used to build these lessons

We used these official documents as references when building this course's lessons. They are the sources for the syllabus structure and course requirements.

College Board · Effective fall 2025