Command Palette

Search for a command to run...

Phase 3Beginner4 of 17 in Core Java

Methods and Arrays

Split programs into named methods, learn exactly how Java passes values, use recursion and arrays, and see where your data lives on the stack and heap.

Until now every program lived inside one long main. Real programs are built from small, named pieces of work called methods, and they keep lists of values in arrays. This phase teaches both from zero: how to write a method, how values travel in and out of it, how Java chooses between methods with the same name, and how a method can call itself (recursion).

It also settles the question that trips up more Java developers than any other: *is Java pass-by-value or pass-by-reference?* (Always by value. You'll see exactly why, with memory diagrams.) Then you'll meet arrays in one and two dimensions, the Arrays helper class, and the picture that ties everything together: the stack of method calls and the heap of objects.

By the end you'll read any method signature, predict what a method call does to your variables, and explain where every value in a running Java program is stored. Phase 4 (Classes and Objects) builds directly on this.

0/11 · 0%
11 topics ~5 h 37 code blocks & diagrams
Start with the first topic
1
3.1

Methods: Naming a Piece of Work

A method is a named block of code you can run again and again by calling its name. Methods let you write a piece of work once, give it a clear name, and reuse it, which keeps programs short, readable and easy to fix.

28 min 4 code practice

2
3.2

Parameters and Return Values

Parameters let a method receive input values, and a return value lets it hand one result back to the caller. Together they turn a method into a reusable little machine: values go in, work happens, a result comes out.

27 min 3 code practice

3
3.3

Java Is Always Pass-by-Value

When you call a method, Java copies the value of each argument into the parameter. For primitives the copy is the number itself; for objects and arrays the copy is the reference (the address), so the method can change the object but can never make the caller's variable point somewhere else.

29 min 4 code practice

4
3.4

Method Overloading

Overloading means giving several methods the same name but different parameter lists. The compiler picks which one to call from the number and types of the arguments, at compile time, following a fixed set of rules.

27 min 3 code practice

5
3.5

Variable Arguments (varargs)

A varargs parameter, written int... nums, lets a method accept any number of arguments of one type, including none. Inside the method it is an ordinary array, which the compiler builds for you at each call.

27 min 3 code practice

6
3.6

Scope and Lifetime of Variables

A variable's scope is the part of the code where its name can be used; its lifetime is how long its storage exists while the program runs. Locals live in a block and die with their method call, fields live as long as their object, and static fields live as long as their class.

28 min 3 code practice

7
3.7

Recursion

Recursion is when a method solves a problem by calling itself on a smaller version of the same problem, until it reaches a case small enough to answer directly. Every recursive method needs a base case that stops it and a recursive case that moves toward it.

34 min 4 code practice

8
3.8

Arrays

An array is a fixed-size, numbered row of values of one type, stored together as a single object on the heap. You reach each value by its index, from 0 to length - 1, in constant time.

33 min 3 code practice

9
3.9

Two-Dimensional and Jagged Arrays

Java has no true grid type: a two-dimensional array is an array whose elements are references to other arrays (the rows). That means rows can have different lengths (jagged arrays), and you index with grid[row][col].

27 min 3 code practice

10
3.10

The Arrays Utility Class

java.util.Arrays is a toolbox of ready-made static methods for arrays: print, sort, search, fill, copy, compare, and turn an array into a list or stream. Using it is shorter, faster and less buggy than writing the same loops yourself.

27 min 3 code practice

11
3.11

Stack and Heap: Where Your Data Lives

A running Java program keeps method calls and their local variables on a per-thread stack, and all objects and arrays on a shared heap. Locals hold primitive values or references; the objects those references point to live on the heap until the garbage collector reclaims them.

31 min 4 code practice