Cheat sheets

AP Computer Science A / Chapter 1

Using Objects and Methods

Cheat sheet

Algorithms and errors

  • Sequence determines which state an observation records.
  • Compilation checks translatable source; tests compare behavior against a specification.
  • Syntax defects stop compilation. Logic defects violate requirements. An unhandled exception interrupts execution.

Representing values

  • int stores whole numbers in a fixed range; double approximates fractional numeric data; boolean stores true or false.
  • A class such as String defines a reference type. Quotation makes digits text rather than a primitive number.
  • A declaration introduces a typed name; Java distinguishes identifiers by letter case.

Arithmetic and output

  • The operand types at division determine whether its fractional part survives.
  • Parentheses group first; multiplication, division and remainder group before addition and subtraction. Equal priority groups left to right.
  • Remainder finds the ungrouped amount for nonnegative dividend and positive divisor. Integer division by zero raises ArithmeticException.
  • print retains the output position; println advances after displaying its argument. Within a literal, \n, \" and \\ encode newline, quote and backslash.

Assignment and input

  • Evaluate the complete right side, then replace the destination’s stored value. A copied primitive value does not track its source.
  • A local variable needs initialization before reading. null is reference absence; empty text is an object with no characters.
  • Input includes tactile, audio, visual and keyboard data; Scanner is one text-input facility.

Conversions and finite storage

  • A cast before division can change the arithmetic; widening the completed int quotient cannot restore its fraction.
  • Casting finite in-range double to int truncates toward zero. Add 0.5 for nonnegative nearest rounding; subtract 0.5 for negative nearest rounding, with half ties away from zero.
  • Four-byte int bounds are Integer.MIN_VALUE and Integer.MAX_VALUE. Out-of-range arithmetic wraps; double approximation instead limits precision.

Numeric updates

  • Compound assignment operates on the current value and stores back into the left-side type.
  • Group the entire right operand when expanding a compound multiplication or division.
  • A separate ++ adds one, while a separate -- subtracts one.

Libraries and documentation

  • Libraries supply reusable classes organized into packages; API documentation describes their usable contracts.
  • Attributes record state; methods supply behaviors. Importing a name does not execute that behavior.
  • Line, block and Javadoc comments document code. A precondition constrains a valid call; a postcondition promises its outcome. Comment text does not enforce either.

Calls and signatures

  • Signatures retain the method name and ordered parameter types, excluding return type and parameter names.
  • Match each argument’s compatible type and position. Distinct parameter signatures permit overloading.
  • Calls execute the body before the caller continues; non-void results can be used in expressions, while void supplies no value.
  • Call by value copies arguments. Changing a primitive parameter leaves the caller’s separate variable intact.
  • static associates a method with its class. Use ClassName.method(arguments) from a client; the qualifier is optional inside the defining class.

Math contracts

  • Math is available through java.lang. abs has int and double overloads; pow and sqrt return double, and sqrt gives the nonnegative root.
  • Math.random returns a double at least zero and below one. Scale first, cast nonnegative bins, then offset for an integer interval.
  • The int minimum has no positive int counterpart, so its absolute value cannot be positive within int.

Objects and receivers

  • A class supplies the common definition; each object is a particular instance with state.
  • Superclass/subclass describes inherited reuse; every Java class descends from Object.
  • new invokes a matching constructor before the next client statement. Copying a reference creates an alias rather than another object.
  • An instance call selects its receiver’s object. Mutations are shared through aliases, but reassigning one reference leaves the others intact.
  • An instance call through null causes NullPointerException and interrupts normal control flow.

String operations

  • String text is immutable; concatenation and substring return results rather than editing an earlier object.
  • With String +, each primitive converts to text. Group numeric arithmetic explicitly when it must precede concatenation.
  • Character indices begin at zero. substring(from,to) includes from and excludes to; equal valid endpoints produce empty text.
  • length counts characters; indexOf returns the first match’s start or -1. A single character at i is substring(i,i+1).
  • equals compares contents. compareTo reports lexicographic order by sign; a shorter matching prefix comes first.
  • Object concatenation uses toString. A class may specialize the inherited public operation with an override having the same signature.

Sign in