Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Classes, Objects and Memory

    Quick Reference

    Glossary and cheat sheet for Classes, Objects and Memory — the page to reread before an interview.

    Extra: Reference5 min read

    Glossary#

    TermPlain meaningChapter
    class / object (instance)blueprint / one thing built from it1
    fielddata each object holds1
    newbuild an object on the heap and return a reference1, 2
    constructorsetup code run by new; class name, no return type2
    this / this(...)the current object / call another constructor2
    default constructorthe free no-arg constructor, gone once you write any2
    instance member / static memberper object / one per class, shared3
    referenceaddress card pointing to an object4
    aliasingtwo references to one object4
    null / NullPointerExceptionno object / using a null reference4
    garbage collection / reachablefreeing unreachable objects / still referred to4
    final variablecan't be reassigned (object may still change)5
    immutable objectstate never changes after construction5
    String pool / intern()shared literal strings / get the pooled copy6
    compareTonegative / zero / positive ordering result6
    StringBuilder / StringBuffermutable text builder / older synchronised version7
    wrapper classobject version of a primitive (Integer…)8
    autoboxing / unboxingautomatic primitive ↔ wrapper conversion8
    Integer cachereused Integer objects for −128..1278
    identity / equality== same object / equals same value9
    toStringhow an object prints9
    package / import / fully qualified namegroup of classes / use by short name / java.util.ArrayList10
    java.langpackage imported automatically10
    Maven / GradleJava build and dependency tools10

    Cheat Sheet#

    Class template

    Java
    class Student {
        private final String name; // private: 03_java_oop.md
        private int score;
        static int created = 0;
    
        Student(String name, int score) {
            this.name = name;
            this.score = score;
            created++;
        }
    
        Student(String name) {
            this(name, 0);
        }
    
        void addBonus(int p) {
            score += p;
        }
    
        boolean isPassing() {
            return score >= 150;
        }
    
        @Override
        public String toString() {
            return name + "(" + score + ")";
        }
    }

    Memory rules

    Output
      objects on the heap · variables hold references · primitives hold values
      b = a copies the reference · methods get a copy of the reference (can mutate, can't re-point)
      unreachable → garbage collected · null.anything → NullPointerException

    Equality

    Output
      strings, wrappers, your objects → .equals          "const".equals(maybeNull) is null-safe
      Integer == works only in −128..127 (cache)         unboxing null → NPE → use getOrDefault

    Strings and builders

    Java
    s = s.trim();                                   // strings never change in place
    char[] c = s.toCharArray(); Arrays.sort(c); new String(c);
    StringBuilder sb = new StringBuilder();
    sb.append(x).insert(0, y).reverse().setCharAt(i, ch); sb.deleteCharAt(i); sb.length(); sb.toString();
    Character.isDigit / isLetter / isLetterOrDigit / toLowerCase · ch - '0' · ch - 'a'

    Try This Yourself#

    1. Replace the parallel arrays. Rewrite the StudyBuddy report from Java Basics Ch 9 with a Student class (name, correct answers, on-time flag) that has a score() and a grade() method and a toString(). Use a Student[] array. Confirm the output is identical.

    2. Prove pass-by-value. Write rename(Student s) and replace(Student s) from Ch 4 and draw the stack/heap diagram for each call before running them.

    3. Trap hunt. Predict, then run: new String("a") == "a", "a" == "a", Integer.valueOf(127) == Integer.valueOf(127), Integer.valueOf(128) == Integer.valueOf(128), Long.valueOf(5).equals(5) (hint: 5 is boxed to Integer, not Long).

    4. Measure. Repeat the += vs StringBuilder timing with 25,000, 50,000 and 100,000 characters. Does the += time roughly quadruple each time n doubles? (That's what O(n²) predicts — Big-O and Complexity.)