JS
JV

JavaScript to Java

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Collections4Classes & OOP5Exception Handling6Async vs Threads7Generics8String Methods9Interfaces and Abstract Classes10Build Tools and Ecosystem
All Mirror Courses
JS
JV
Functions & Methods
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with defining reusable behavior.

JV
In Java:

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:

JV
Java 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:

JS
JavaScript (What you know)
// 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);
}
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java methods must be inside a class; JS functions can be free-standing

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java has no default parameters — use overloading instead

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java varargs (int... nums) replace JS rest params (...nums)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

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.

JS
JavaScript
function add(a, b) { return a + b; }
JV
Java
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.

JS
JavaScript
function greet(name = "World") { return "Hello, " + name; }
JV
Java
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.

JS
JavaScript
const double = x => x * 2;
JV
Java
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)
Common Pitfall
Don't assume Java works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Java to practice these concepts.
PreviousNext