Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

This AP CSA Java reference sheet covers the core Java syntax, object-oriented ideas, data structures, traversal patterns, and standard library methods students need most often. It helps students quickly review how classes, methods, arrays, ArrayList, loops, and algorithms fit together in AP Computer Science A. A clear reference is useful during practice because small syntax details can change how a program compiles or runs.

The most important ideas are that objects store state in instance variables, methods define behavior, and constructors initialize new objects. Arrays have fixed length, ArrayList objects can change size, and loops are used to visit elements safely. AP CSA algorithms often use accumulation, counting, searching, finding a maximum or minimum, and comparing objects with equals instead of ==.

Key Facts

  • A class defines a type, and an object is created with ClassName obj = new ClassName(arguments);.
  • A constructor has the same name as the class, has no return type, and initializes instance variables.
  • Instance variables are usually private, while public accessor and mutator methods provide controlled access.
  • A method header follows the pattern accessModifier returnType methodName(parameterList), such as public int getScore().
  • An array is created with Type[] name = new Type[size];, and its valid indexes are 0 through name.length - 1.
  • An ArrayList is created with ArrayList<Type> list = new ArrayList<Type>();, and its size is found with list.size().
  • Use list.get(i), list.set(i, value), list.add(value), list.add(i, value), and list.remove(i) to work with ArrayList elements.
  • Use s.equals(t) to compare String contents, because s == t checks whether two references point to the same object.

Vocabulary

Class
A class is a blueprint that defines the instance variables, constructors, and methods for a type of object.
Object
An object is an instance of a class that has its own data and can use the class methods.
Constructor
A constructor is a special block of code that runs when an object is created and sets up its initial state.
Array
An array is a fixed-size sequence of values of the same type accessed using integer indexes.
ArrayList
An ArrayList is a resizable list from the Java library that stores object references and supports methods like add, get, set, and remove.
Traversal
A traversal is the process of visiting each element in a data structure, often with a for loop or enhanced for loop.

Common Mistakes to Avoid

  • Using == to compare Strings, which is wrong because == compares object references instead of text contents. Use str1.equals(str2) when checking whether two Strings contain the same characters.
  • Looping with i <= arr.length, which is wrong because the last valid array index is arr.length - 1. Use i < arr.length to avoid an ArrayIndexOutOfBoundsException.
  • Removing items from an ArrayList while moving forward through indexes, which can skip elements because later elements shift left. Traverse backward or adjust the index after a removal.
  • Forgetting that integer division truncates decimals, which gives wrong results when both operands are int. Cast one value to double, such as (double) total / count, when a decimal result is needed.
  • Writing a constructor with a return type, which makes it a regular method instead of a constructor. A constructor must have the class name and no return type.

Practice Questions

  1. 1 What is printed by this code: int x = 17 / 5; int y = 17 % 5; System.out.println(x + "," + y);
  2. 2 An int[] nums = {4, 9, 2, 9, 6};. Write a loop that counts how many values are greater than 5.
  3. 3 An ArrayList<String> names contains ["Ana", "Bo", "Cy"]. What are the contents after names.add(1, "Dee"); names.remove(3);?
  4. 4 Explain why a private instance variable should usually be accessed through public methods instead of being made public.

Understanding AP CSA Java Reference Sheet

Java programs become easier to reason about when you separate values from references. A variable of a primitive type holds its actual value. A variable for an object usually holds a reference to where that object is stored.

This means two variables can refer to one shared object. Changing that object through one variable can be seen through the other variable. This is called aliasing, and it explains many surprising results in tracing problems.

A reference can also be null, meaning it refers to no object. Trying to call a method through null causes a runtime error. Good constructors and mutator methods help keep an object in a valid state from the moment it is created.

Indexes are one of the main sources of errors in Java. An index is a position, not a count. The first position has index zero, so the final valid position is one less than the number of elements.

A loop that reaches the size itself goes too far and causes an out of bounds error. Before writing a loop, decide exactly which elements it must visit. Some loops need every element.

Others need pairs of neighbors, so they must stop earlier. An enhanced for loop is useful when only reading each element. An indexed loop is better when the position matters, when comparing nearby items, or when changing a particular location.

Changes to an ArrayList have an important side effect. When an element is removed, later elements shift left to fill the gap. A forward loop can then skip an element because the next item moves into the current position just after the loop advances.

When removing several matching items, traversing from the end toward the beginning is often safer. ArrayList uses object types, so whole number values are stored as Integer objects rather than the primitive int type.

Java usually converts between these forms automatically. Students should still notice the difference when reading method descriptions and compiler messages.

Methods are best understood as small contracts. The parameters describe what information a method receives. Its return type tells what result, if any, comes back to the caller.

A void method performs an action without returning a value. Java passes every argument by value. For an object argument, the copied value is a reference.

A method can therefore change the shared object, but assigning its parameter to a different object does not change the caller variable. When tracing an algorithm, use a table for loop index, current element, accumulator, and result. Pay close attention to starting values.

A maximum search needs a sensible initial maximum, and a search method needs a clear value to return when no match is found. String content comparisons need equals because two separate String objects may contain identical text.