Java Bootcamp – Workbooks and Challenges

Cheat Sheet

This cheat sheet contains the most important takeaways that lead up to section nine.


  • Class: blueprint from which you can create objects.

  • Object: an object is a thing that you can see or describe.

  • Field: class variable that describes an object.

  • Action: method (function) that represents what the object can do.

  • Constructor: runs when you create a new object.

    • Purpose: update every field of the newly created object.
  • this: refers to the current object that's calling the constructor or method.

    • Purpose: useful when fields and parameters have conflicting names.
  • Copy Constructor: runs when you create an object.

    • Purpose: copy every value from a source object into a new object.
  • Getter: method (function) that returns a copy of a private field's value.

  • Setter: method (function) that updates the value of a private field.

  • toString: method that returns a String representation of every field in an object.

    • Purpose: When you print an object, Java internally calls the toString method.

public vs. private

  • public: provides public access to a field or method from anywhere.

    • Constructors and methods tend to be public.
  • private: prevents direct access of a field or method outside of its class.

    • To protect the state of an object, fields tend to be private.

The Big Three

If a class has fields, you need to add:

  1. Constructor -- to update the fields of a new object.

  2. Getters -- to get the value of a private field.

  3. Setters -- to set the value of a private field.


Screen Shot 2021-06-19 at 2.53.23 PM.png

  1. Continue: continues to the next breakpoint.
  2. Step over: steps over a line (see Debugging Exercise 3).
  3. Step into: steps into a function/cons.
  4. Step out: steps out of a function/cons.
  5. Restart: restarts the runtime.
  6. Stop: stops the runtime



  • Compile-time.
  • Outside the application's control.
  • Java forces you to try-catch the potential failure.


  • Runtime.
  • Results from badly written code.
  • Do not catch unchecked exceptions. Instead, fix your code.

Throwing Exceptions:

  • Forces the caller to improve their code.
  • Throw an IllegalArgumentException if the caller passes illegal values into a method/constructor.
  • Throw an IllegalStateException if the caller invokes a method at a bad time (object not in a valid state).

Mutable / Immutable Objects

  • Mutable class: class with accessible mutators (setters).
  • Immutable class: class without accessible mutators (setters).

Screen Shot 2021-06-18 at 11.42.14 PM.png

Example 1:

An array is a mutable object.

Every array is a mutable object of a type[] class.


It follows that arrays are vulnerable to the reference trap.

Example 2:

String is an immutable class.

It follows that objects of the String class are immune to the reference trap.

Reference Trap

The state of a variable should not change because you updated another.

Screen Shot 2021-06-18 at 11.54.20 PM.png

Falling into a reference trap can:

  1. lead to unpredictable behavior.

  2. lead to variables changing when you don't expect them to.

  3. leave you wondering: why is my code doing this?

Wrapper class

Wrapper: Immutable class that wraps around a primitive.

Screen Shot 2021-06-19 at 12.05.42 AM.png

String is not a wrapper class. But, it is immutable.


  • Use primitive when possible (faster and less memory).
  • Use wrapper only when you need to (inside ArrayList...).

Array vs. ArrayList vs. HashMap

  • Array

    • fixed.
    • stores primitives and objects.
    • type[] values = new type[]
  • ArrayList

    • resizable.
    • stores objects.
    • ArrayList<object> values = new ArrayList<object>();
  • HashMap

    • resizable.
    • stores key-value pairs.
    • HashMap<Object, Object> pairs = new HashMap<Object, Object>()

Rule: use arrays when size is fixed (less overhead). Use ArrayList when size can vary. Use HashMap when there is parity between data.

Test-Driven Development.

  1. Identify meaningful test cases.
  2. Write a unit test for each test case.
    • Write code to make the test fail.
    • Write code to make the test pass.
    • Refactor if necessary.

@Test annotates a unit test. @Before annotates a method that runs before each test.

Stream Pipeline

Instead of loops, you should pass elements into a pipeline: .intermediateOperation(lambda expression) .intermediateOperation(lambda expression) .intermediateOperation(lambda expression) .intermediateOperation(lambda expression) .terminalOperation(lambda expression)

Intermediate operation: processes the sequence of elements and continues the pipeline.
Terminal operation: ends the pipeline and may return a final value.
Lambda expression syntax:

((parameter) -> {
  code goes here

equals() method

public boolean equals(Object obj) {

    return false if parameter is null.
    return false if parameter isn't instance of class.

    typecast the object.
    compare fields from both objects and return the result.

hashCode() method

When you add an equals() method, you must always add a hashCode() method.

public int hashCode() {
     return Objects.hash(fields go here);    

Naming Conventions

  • class: CamelCase
  • variable: lowerCamelCase
  • method: lowerCamelCase
  • constant: UPPER_SNAKE_CASE

static and final

  • static: variable or method belongs to the class.
  • final: cannot be updated.

Feedback Summary
43 students





Written Reviews
There are no written reviews yet.