2) Java Basics
Goal
Learn the building blocks: variables, types, operators, and basic console output.
The mental model
- Java is statically typed: variables have a declared type.
- Java is compiled to bytecode and run on the JVM.
Variables and primitive types
Common primitives:
int (32-bit integer)
long (64-bit integer)
double (floating point)
boolean (true/false)
char (single UTF-16 code unit)
Example:
int age = 25;
long population = 8_000_000_000L;
double price = 19.99;
boolean active = true;
char grade = 'A';
var (local type inference)
var name = "Sam"; // inferred as String
Use var when it improves readability, not when it hides meaning.
Reference types
String is a reference type (an object)
Operators (the important ones)
- Arithmetic:
+ - * / %
- Comparisons:
== != < <= > >=
- Boolean logic:
&& || !
Important:
- For objects (like
String), use .equals() not ==.
String a = "hi";
String b = new String("hi");
System.out.println(a.equals(b)); // true
System.out.println(a == b); // usually false
Printing
System.out.println("Hello");
System.out.printf("Age: %d%n", age);
Exercises
- Create variables of each primitive type and print them.
- Convert Celsius to Fahrenheit: $F = C \times \frac{9}{5} + 32$.
- Compare two strings using both
== and .equals() and observe the difference.
Table of contents
- Getting Started: Install, run, and your first program
- Java Basics: types, variables, operators, formatting
- Control Flow: if/switch/loops
- Methods: parameters, return values, overloading
- OOP: classes, objects, encapsulation
- Inheritance & Polymorphism (and when not to use them)
- Interfaces, abstract classes, and design basics
- Exceptions and error handling
- Strings, files, and I/O basics
- Collections: List/Set/Map and Big-O intuition
- Generics (the useful parts)
- Lambdas & Streams
- Dates and time (java.time)
- Testing with JUnit 5 (basics)
- Concurrency: threads, executors, futures
- JVM basics: memory, GC, performance habits
- Build tools: Maven essentials (recommended)
- Next steps: projects to build