java-tutorial

2) Java Basics

Goal

Learn the building blocks: variables, types, operators, and basic console output.

The mental model

Variables and primitive types

Common primitives:

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 city = "Colombo";

Operators (the important ones)

Important:

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

  1. Create variables of each primitive type and print them.
  2. Convert Celsius to Fahrenheit: $F = C \times \frac{9}{5} + 32$.
  3. Compare two strings using both == and .equals() and observe the difference.

Table of contents

  1. Getting Started: Install, run, and your first program
  2. Java Basics: types, variables, operators, formatting
  3. Control Flow: if/switch/loops
  4. Methods: parameters, return values, overloading
  5. OOP: classes, objects, encapsulation
  6. Inheritance & Polymorphism (and when not to use them)
  7. Interfaces, abstract classes, and design basics
  8. Exceptions and error handling
  9. Strings, files, and I/O basics
  10. Collections: List/Set/Map and Big-O intuition
  11. Generics (the useful parts)
  12. Lambdas & Streams
  13. Dates and time (java.time)
  14. Testing with JUnit 5 (basics)
  15. Concurrency: threads, executors, futures
  16. JVM basics: memory, GC, performance habits
  17. Build tools: Maven essentials (recommended)
  18. Next steps: projects to build