JAVA — CHAPTER 5

Methods · Concept Cheat Sheet
Declaring Methods Overloading Scope Math & Packages Call Stack enum
1 DECLARING & CALLING METHODS
public static int square(int number) { return number * number; } // calling it: int result = square(5); // 25

Anatomy of a method

  • Modifierspublic static
  • Return typeint, or void if nothing returned
  • Method namesquare
  • Parameter list(int number)
  • Method body — the code block, with a return if non-void
2 CASE STUDY: RANDOM NUMBERS & enums
Random randomNumbers = new Random(); int face = 1 + randomNumbers.nextInt(6); // 1–6 enum Status { WON, LOST, CONTINUE } Status game = Status.CONTINUE;

Key points

  • Random.nextInt(n) gives 0 to n-1 → shift with +1 for dice-style ranges
  • enum defines a fixed, named set of constants — safer than raw ints for "modes" or "states"
  • Great for simulations: dice games, card games, state machines
3 SCOPE OF DECLARATIONS

Local scope

A variable declared inside a method (or block) exists only within that block.

Field / class scope

Instance variables declared in a class are visible to all of that class's methods.

Shadowing

A local variable with the same name as a field hides the field inside that method.

4 METHOD OVERLOADING
int add(int a, int b){ return a+b; } double add(double a, double b){ return a+b; } int add(int a, int b, int c){ return a+b+c; }

Rule

Multiple methods can share the same name as long as their parameter lists differ (number or types of params). The compiler picks the right one to call based on the arguments given — this is called the method's signature.

5 CLASS Math & JAVA API PACKAGES
Math methodPurpose
Math.sqrt(x)square root
Math.pow(x,y)x raised to power y
Math.abs(x)absolute value
Math.max/min(a,b)larger/smaller value

Packages

  • Related classes are grouped into packages (e.g. java.util, java.math)
  • Import a class with import java.util.Random;
  • Math's methods are static — call them via the class name, no object needed
6 METHOD-CALL STACK & ACTIVATION RECORDS
main() — waiting
calculate() — waiting
square() — running now

How it works

  • Each method call pushes a new activation record (stack frame) with its local variables/parameters
  • When a method returns, its frame is popped off — control returns to the caller
  • Last-In-First-Out (LIFO) order — just like Java's Stack class
  • Too many nested calls (e.g. infinite recursion) → StackOverflowError
7 ARGUMENT PROMOTION & CASTING

Implicit promotion

A smaller/less-precise type widens automatically to match a parameter — e.g. passing an int where a method expects a double.

Explicit casting

double avg = 7.8; int whole = (int) avg; // 7 — narrowing needs a cast
8 OBJECTS-NATURAL CASE STUDY: JAVA DATE/TIME API
LocalDate today = LocalDate.now(); LocalDate meeting = LocalDate.of(2026, 9, 20); long daysLeft = ChronoUnit.DAYS.between(today, meeting);

Key classes (java.time)

  • LocalDate — a calendar date, no time/timezone
  • LocalTime / LocalDateTime — time-of-day / both combined
  • Period / Duration — amounts of time
  • Replaces the old, error-prone Date/Calendar classes
9 METHOD REFERENCE TABLE — class Math (FULL)
MethodPurpose
Math.abs(x)absolute value
Math.ceil(x)rounds up to nearest whole
Math.floor(x)rounds down to nearest whole
Math.round(x)rounds to nearest whole
Math.max(a,b)/min(a,b)larger/smaller value
Math.pow(x,y)x raised to power y
Math.sqrt(x)square root
Math.random()double in range [0.0, 1.0)
Random Class MethodPurpose
nextInt()random int, full range
nextInt(n)random int from 0 to n-1
nextDouble()random double [0.0, 1.0)
nextBoolean()random true/false