Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| class / object (instance) | blueprint / one thing built from it | 1 |
| field | data each object holds | 1 |
new | build an object on the heap and return a reference | 1, 2 |
| constructor | setup code run by new; class name, no return type | 2 |
this / this(...) | the current object / call another constructor | 2 |
| default constructor | the free no-arg constructor, gone once you write any | 2 |
| instance member / static member | per object / one per class, shared | 3 |
| reference | address card pointing to an object | 4 |
| aliasing | two references to one object | 4 |
null / NullPointerException | no object / using a null reference | 4 |
| garbage collection / reachable | freeing unreachable objects / still referred to | 4 |
final variable | can't be reassigned (object may still change) | 5 |
| immutable object | state never changes after construction | 5 |
String pool / intern() | shared literal strings / get the pooled copy | 6 |
compareTo | negative / zero / positive ordering result | 6 |
| StringBuilder / StringBuffer | mutable text builder / older synchronised version | 7 |
| wrapper class | object version of a primitive (Integer…) | 8 |
| autoboxing / unboxing | automatic primitive ↔ wrapper conversion | 8 |
| Integer cache | reused Integer objects for −128..127 | 8 |
| identity / equality | == same object / equals same value | 9 |
toString | how an object prints | 9 |
| package / import / fully qualified name | group of classes / use by short name / java.util.ArrayList | 10 |
java.lang | package imported automatically | 10 |
| Maven / Gradle | Java build and dependency tools | 10 |
Cheat Sheet#
Class template
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
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 → NullPointerExceptionEquality
strings, wrappers, your objects → .equals "const".equals(maybeNull) is null-safe
Integer == works only in −128..127 (cache) unboxing null → NPE → use getOrDefaultStrings and builders
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#
Replace the parallel arrays. Rewrite the StudyBuddy report from Java Basics Ch 9 with a
Studentclass (name, correct answers, on-time flag) that has ascore()and agrade()method and atoString(). Use aStudent[]array. Confirm the output is identical.Prove pass-by-value. Write
rename(Student s)andreplace(Student s)from Ch 4 and draw the stack/heap diagram for each call before running them.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:5is boxed toInteger, notLong).Measure. Repeat the
+=vsStringBuildertiming 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.)