PNG image of Steve Brandt

Steven R. Brandt

Cheat Sheet

Objects

An object describes a new data type with its own fields and methods.
  1. Have constructors
  2. Have a toString() method
  3. Have an equals(Object) method
  4. Operator == compares if classes are the same instance
  5. May have a value of null
  6. Are passed by reference
  7. Fields without keyword static are by instance
  8. A class can extend from only one class
  9. If extends not used, then the class extends java.lang.Object
  10. All classes extend from java.lang.Object
  11. If class A extends B, then A is the subclass and B is the superclass
  12. If class A extends B, then A inherits from B
  13. Add @Override for checking that a method overrides

Primitive Types

  1. There are 8:
    1. boolean,
    2. byte,
    3. char,
    4. short,
    5. int,
    6. long,
    7. float,
    8. double
  2. Are passed by value

Interfaces

An interface is a specification for a class, with no implementation.
  1. Interface names are type names
  2. Interfaces may not be instantiated
  3. All interface methods are public
  4. Interface only have methods, and the methods do not have bodies, i.e. they have ";" instead of "{}"
  5. You can implement any number of interfaces

Casting

A cast changes one type to another. For objects, this only changes the type of the reference. For primitive types, it changes the value.
  1. int n = (int)3.14; converts a double to an integer.
  2. A a = new B() allowed only if B extends A.
  3. A a = (A)new B() is a cast. The compiler will allow it, but the runtime may not.
  4. if(B instanceof A) checks if the runtime will allow the cast.

toString() methods

  1. Don't print, return a String
  2. Use String.format()

Arrays

  1. Use StringBuilder (see Alist.java
  2. Autoboxing: int => Integer, short => Short, byte => Byte, char => Character, etc.
  3. Number is superclass of Integer, Short, Double, etc.
  4. assert statement: enabled by -ea.
  5. Understanding error messages: the stack and stack traces.

More Arrays

  1. Scope of variables
  2. List and ArrayList classes (see ArList.java
  3. Packages (see UseArList.java
  4. The stack and exceptions (see Npe.java

Still More Arrays

  1. Enhanced For Loops
  2. Interface Iterable, see Alist2.java
  3. Interface Iterator, see Alist2.java