PY
JV

Python to Java

11 lessons

Progress0%
1Introduction2Variables & Types3Functions to Methods4Lists to Arrays5Dicts to Maps6Classes & OOP7Inheritance8Exception Handling9Modules to Packages10Ecosystem11Modern Java Features
All Mirror Courses
PY
JV
Functions to Methods
MirrorLesson 3 of 11
Lesson 3

Functions to Methods

Functions to Methods

Introduction

In this lesson, you'll learn about functions to methods in Java. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with functions to methods.

JV
In Java:

Java has its own approach to functions to methods, 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 Greeter {

    // No default params — use overloading
    static String greet(String name) {
        return greet(name, "Hello");
    }

    static String greet(String name, String greeting) {
        return greeting + ", " + name + "!";
    }

    // *args equivalent: varargs
    static int total(int... args) {
        int sum = 0;
        for (int n : args) sum += n;
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(greet("Alice"));
        System.out.println(total(1, 2, 3));
    }
}

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
# Standalone function with default parameter
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

# *args — variable positional arguments
def total(*args):
    return sum(args)

# **kwargs — variable keyword arguments
def configure(**kwargs):
    for key, val in kwargs.items():
        print(f"{key} = {val}")

print(greet("Alice"))
print(total(1, 2, 3))
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python uses def; Java declares the return type instead (void, int, String, …)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python default parameters → Java method overloading

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python *args → Java varargs (Type... name)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python **kwargs has no direct Java equivalent

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java methods must live inside a class

Step-by-Step Breakdown

1. Method Signature

Java methods declare their return type before the name. void means nothing is returned.

PY
Python
def greet(name: str) -> str:
    return "Hello, " + name
JV
Java
static String greet(String name) {
    return "Hello, " + name;
}

2. Default Parameters via Overloading

Java has no default parameter values. Simulate them by overloading — writing two methods with the same name but different signatures.

PY
Python
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"
JV
Java
static String greet(String name) {
    return greet(name, "Hello");
}
static String greet(String name, String greeting) {
    return greeting + ", " + name + "!";
}
Rule of Thumb
Delegate the shorter overload to the longer one to avoid duplicating logic.

3. Varargs (like *args)

Java varargs (Type... name) lets a method accept any number of arguments, just like Python's *args.

PY
Python
def total(*args):
    return sum(args)
JV
Java
static int total(int... args) {
    int sum = 0;
    for (int n : args) sum += n;
    return sum;
}

4. No **kwargs in Java

Java has no keyword-argument mechanism. Pass a Map<String, Object> or a dedicated config object instead.

PY
Python
def configure(**kwargs):
    print(kwargs)
JV
Java
static void configure(Map<String, Object> opts) {
    opts.forEach((k, v) -> System.out.println(k + " = " + v));
}
Common Pitfall
There is no Java equivalent of **kwargs. Design method signatures with explicit parameters or a config object.

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python uses def; Java declares the return type instead (void, int, String, …)
  • Python default parameters → Java method overloading
  • Python *args → Java varargs (Type... name)
Common Pitfall
Don't assume Java works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Return type replaces def — void for no return value
  • Default params → overloading with delegation
  • Varargs (int... args) ≈ *args
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Java to practice these concepts.
PreviousNext