Cheat Sheet
This cheat sheet contains the most important takeaways that lead up to section eight.
Definitions
-
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 anew
object.
- Purpose: copy every value from a
-
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 aString
representation of every field in an object.- Purpose: When you print an object, Java internally calls the
toString
method.
- Purpose: When you print an object, Java internally calls the
public
vs. private
-
public
: provides public access to a field or method from anywhere.- Constructors and methods tend to be
public
.
- Constructors and methods tend to be
-
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
.
- To protect the state of an object, fields tend to be
The Big Three
If a class
has fields, you need to add:
-
Constructor -- to update the fields of a
new
object. -
Getters -- to get the value of a
private
field. -
Setters -- to set the value of a
private
field.
Debugging
- Continue: continues to the next breakpoint.
- Step over: steps over a line (see Debugging Exercise 3).
- Step into: steps into a function/cons.
- Step out: steps out of a function/cons.
- Restart: restarts the runtime.
- Stop: stops the runtime
Exceptions
Checked:
- Compile-time.
- Outside the application's control.
- Java forces you to
try-catch
the potential failure.
Unchecked:
- 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).
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.
Falling into a reference trap can:
-
lead to unpredictable behavior.
-
lead to variables changing when you don't expect them to.
-
leave you wondering: why is my code doing this?
Wrapper class
Wrapper: Immutable class that wraps around a primitive.
String
is not a wrapper class. But, it is immutable.
Rule:
- Use primitive when possible (faster and less memory).
- Use wrapper only when you need to (inside
ArrayList
...).
Array
vs. ArrayList
-
Array
- fixed
- stores primitives and objects
type[] values = new type[]
-
ArrayList
- resizable
- stores objects
ArrayList<object> values = new ArrayList<object>();
Rule: use arrays when size is fixed (less overhead). Use ArrayList
when size can vary.