Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| JDK / JRE / JVM | dev kit / runtime (JVM + library) / the bytecode runner | 1 |
| LTS | long-term support Java version (8, 11, 17, 21, 25) | 1 |
| class / main method | container for code / where running starts | 1 |
| statement / block | one instruction ending in ; / code inside { } | 1 |
| compile error / runtime error (exception) | caught by javac / happens while running | 1 |
| variable / type | named box / what kind of value it holds | 2 |
| declare / assign / initialise | create the box / put a value in / both at once | 2 |
| statically typed | types fixed and checked at compile time | 2 |
| primitive type | byte short int long float double boolean char | 2 |
| literal | a value written in code (200, 9L, 'A', "A") | 2 |
final / constant | assign once / a fixed named value | 2 |
| camelCase / PascalCase / UPPER_SNAKE_CASE | variables & methods / classes / constants | 2 |
| operator / expression | combines values / anything producing a value | 3 |
integer division / modulo (%) | drops the fraction / the remainder | 3 |
| pre- / post-increment | ++i changes first / i++ gives old value first | 3 |
| short-circuit | &&/|| skip the right side when the answer is known | 3 |
| widening / narrowing / cast | small→big automatic / big→small / explicit (type) conversion | 3 |
| String / concatenation | text object / joining with + | 4 |
| format specifier | %d %s %.2f %n placeholders in printf | 4 |
| parsing | text → number (Integer.parseInt) | 4 |
| if / else if / else / ternary / switch | decisions | 5 |
| fall-through | old switch running into the next case without break | 5 |
| for / while / do-while / for-each | loops | 6 |
| break / continue / label | leave loop / next round / exit an outer loop | 6 |
| off-by-one error | loop runs one step too many or too few | 6 |
| method / signature | named reusable code / its name + parameters | 7 |
| parameter / argument | declared input / actual value passed | 7 |
return type / void | type of the result / no result | 7 |
| scope | where a variable exists | 7 |
| pass-by-value | the variable's value (number or reference) is copied | 7 |
| overloading | same method name, different parameter lists | 7 |
| array / element / index | fixed row of same-type values / one value / its 0-based position | 8 |
null | "refers to nothing" | 8 |
| ArrayIndexOutOfBoundsException | reading/writing outside 0..length-1 | 8 |
| aliasing | two variables referring to the same array | 8 |
| 2-D array | an array of arrays (a grid) | 8 |
| stdin / stdout, BufferedReader, StringBuilder | input/output streams, fast reader, fast string building | 9 |
Cheat Sheet#
Skeleton
import java.util.*;
public class Main {
public static void main(String[] args) {
// ...
}
}
// run: java Main.javaTypes and limits
int ±2.1e9 (default) · long ±9.2e18 (9_000_000_000L) · double (default decimal) · boolean · char ('A' = 65)
Integer.MAX_VALUE / MIN_VALUE · Long.MAX_VALUEOperator traps
7 / 2 = 3 · 7.0 / 2 = 3.5 · -7 % 3 = -1 · Math.floorMod(-7, 3) = 2
i++ old value · ++i new value · (int) 3.99 = 3 · (double) a / b for averages
c - 'a' → 0..25 · (char)('a' + k) → k-th letter · safe mid: lo + (hi - lo) / 2
"x" + 1 + 2 = "x12" · strings: a.equals(b), never ==Loops
for (int i = 0; i < n; i++) // forward
for (int i = n - 1; i >= 0; i--) // backward
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) // all pairs
while (n > 0) { n /= 10; } // digits
for (int x : arr) // values onlyArrays
int[] a = new int[n]; int[] b = {1, 2, 3}; int[][] g = new int[rows][cols];
a.length Arrays.toString(a) Arrays.sort(a) Arrays.fill(a, -1) Arrays.copyOf(a, n) Arrays.deepToString(g)
int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; // grid neighbours
int[] count = new int[26]; count[c - 'a']++; // letter countsStrings
s.length() s.charAt(i) s.substring(a, b) s.indexOf("x") s.equals(t) s.split(" ")
Integer.parseInt("42") String.valueOf(42) System.out.printf("%d %.2f %s%n", n, d, s);Fast I/O (competitive programming)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
StringBuilder out = new StringBuilder(); ... System.out.print(out);Try This Yourself#
Type and run every program in this file. For each, predict the output first; when you're wrong, find which chapter explains why.
FizzBuzz, then extend it. Print 1 to 30, but "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for both. Then write a method
static String fizz(int n)that returns the word instead of printing it, and call it from a loop.Extend the StudyBuddy report. (a) Print how many students passed (≥
PASS_MARK). (b) Handle ties: print every student who has the top score. (c) Count how many students got each grade using anint[3]. (d) Read the names and answer counts from stdin using the fast input template.Grid practice. Create a 5×5
int[][]grid, set a few cells to 1, and for the centre cell print how many of its 4 neighbours are 1 — using thedirspattern with the bounds check.