4) Methods
Goal
Write reusable code with methods, parameters, return values, and overloading.
A simple method
static int add(int a, int b) {
return a + b;
}
Call it:
void methods
static void greet(String name) {
System.out.println("Hello, " + name);
}
Method overloading
Same method name, different parameter types or counts:
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
Pass-by-value (important!)
Java is pass-by-value.
- For primitives: the value is copied.
- For references: the reference value is copied (two variables can point to the same object).
Exercises
- Write
max(int a, int b).
- Write
isPrime(int n).
- Write
reverse(String s).
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