Part 1 recap: Java fundamentals¶
☕ Java 17
Everything from Part 1 on one page. Each block links back to its section if you want the details.
How Java runs your code¶
.java source → javac → .class bytecode → the JVM runs it on any machine. Install the JDK: it contains the JVM, the libraries and the tools (javac, java, jar, javadoc). javac takes a file name (javac Movie.java); java takes a class name (java com.netflix.Main). → How Java Works
Classes and the main method¶
A class is a blueprint: fields hold data, methods define behavior. A public class lives in a file with exactly its name, and a file holds at most one public class. A program starts in public static void main(String[] args); String... and swapping public/static are also valid. → Your First Java Class · The main Method
Packages and imports¶
package first, then imports, then the class. A package matches its folder (com.netflix.content → com/netflix/content/). A wildcard import never covers sub-packages, java.lang is imported automatically, and a name clash is solved with a fully qualified name. → Packages and Imports
Objects and constructors¶
new creates an object on the heap and runs a constructor: same name as the class, no return type. With no constructor written, the compiler adds an empty no-argument one; write any constructor and it disappears. Inline field values are set before the constructor body runs. → Creating Objects
Primitives and references¶
| Primitives | References | |
|---|---|---|
| What | 8 types: boolean, byte, short, int, long, float, double, char |
everything else: String, Movie… |
| Holds | the value | an object's address |
null |
never | possible |
== |
compares values | compares addresses (use equals() for content) |
Literals: L for long, f for float, underscores only between digits. → Primitives and References
Variables¶
Identifiers start with a letter, $ or _, contain no hyphen and aren't keywords or a lone _. Conventions: camelCase, PascalCase, SCREAMING_SNAKE_CASE. var infers a local variable's type; it needs a non-null value and one variable per declaration. final allows a single assignment. → Declaring Variables
Initialization and scope¶
Fields get defaults (0, false, null); local variables don't and must be assigned on every path before use; final fields must be assigned once, in an initializer or every constructor. A variable lives from its declaration to the end of its block; loop counters die with the loop; a name can't be redeclared while still in scope. → Initializing Variables · Variable Scope
Garbage collection¶
An object becomes eligible for collection when no reference can reach it: references reassigned, set to null, or out of scope. The JVM decides when to collect; System.gc() is only a hint, and collection may never happen at all. → Destroying Objects
What's next¶
Part 2, Expressions & Computations, covers Java's operators: arithmetic, comparison, logical, ternary and assignment, with numeric promotion and casting.
Code: Java17_Part_1_Fundamentals_Core_Syntax on GitHub
🎓 Preparing the OCP exam? Test yourself with the Part 1 practice questions (optional).