Functions & Methods
Defining reusable behavior
Introduction
In this lesson, you'll learn about functions & methods in Java. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with defining reusable behavior.
Java has its own approach to defining reusable behavior, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
public class MathUtils {
// Methods must live in a class
public static int add(int a, int b) {
return a + b;
}
// Overloading instead of default args
public static String greet() { return greet("World"); }
public static String greet(String name) {
return "Hello, " + name + "!";
}
// Varargs (like JS rest params)
public static int sum(int... nums) {
int total = 0;
for (int n : nums) total += n;
return total;
}
// Lambda (Java 8+)
java.util.function.BiFunction<Integer, Integer, Integer> multiply =
(a, b) -> a * b;
}Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
// Free-standing function
function add(a, b) { return a + b; }
// Arrow function
const multiply = (a, b) => a * b;
// Default arguments
function greet(name = "World") {
return "Hello, " + name + "!";
}
// Rest params
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0);
}You may be used to different syntax or behavior.
Java methods must be inside a class; JS functions can be free-standing
You may be used to different syntax or behavior.
Java has no default parameters — use overloading instead
You may be used to different syntax or behavior.
Java varargs (int... nums) replace JS rest params (...nums)
You may be used to different syntax or behavior.
Java lambdas use -> syntax like JS arrow functions =>
Step-by-Step Breakdown
1. Methods in Classes
Java has no standalone functions. All code lives in classes. Use static methods in a utility class to group related functions.
function add(a, b) { return a + b; }public static int add(int a, int b) { return a + b; }2. Overloading vs Defaults
Java doesn't support default parameter values — instead define multiple methods with the same name but different signatures.
function greet(name = "World") { return "Hello, " + name; }public static String greet() { return greet("World"); }
public static String greet(String name) { return "Hello, " + name; }3. Lambdas
Java 8+ lambdas use -> syntax and functional interfaces (BiFunction, Predicate, etc.) for passing behavior as values.
const double = x => x * 2;java.util.function.Function<Integer, Integer> doub = x -> x * 2;Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- Java methods must be inside a class; JS functions can be free-standing
- Java has no default parameters — use overloading instead
- Java varargs (int... nums) replace JS rest params (...nums)
Key Takeaways
- Java methods live in classes; no free functions
- Method overloading replaces default parameters
- Varargs replace JS rest parameters
- Java 8+ lambdas: (a, b) -> a + b