Java Tutorial

Java Method Overloading

Overloading means the same method name with different parameter lists. The compiler picks the match from the arguments you pass.

Same name, different parameters

You can declare several methods named print or add as long as their parameter lists differ in number or type. At the call site you write one name; the compiler chooses the right version.

Example

public class Main {
  static int add(int a, int b) {
    return a + b;
  }

  static double add(double a, double b) {
    return a + b;
  }

  public static void main(String[] args) {
    System.out.println(add(2, 3));       // int version
    System.out.println(add(2.5, 3.1));   // double version
  }
}

Different arity counts too

Changing how many parameters you take is also overloading. That is how you offer a short form and a longer form without inventing awkward names like addTwo and addThree.

Example

public class Main {
  static void greet(String name) {
    System.out.println("Hi, " + name);
  }

  static void greet(String name, int times) {
    for (int i = 0; i < times; i++) {
      System.out.println("Hi, " + name);
    }
  }

  public static void main(String[] args) {
    greet("Ada");
    greet("Ada", 3);
  }
}

Return type alone is not enough

Two methods that differ only in return type are not overloads — they are a compile error. The signature that matters is the name plus the parameter types. The return type is not part of that choice.

Example — this does not compile

static int value() { return 1; }
static double value() { return 1.0; }   // error: same signature

Overloading is chosen at compile time from the argument types. Overriding (inheritance) is chosen atruntime from the object's real class. Do not mix the two ideas.

Try It Yourself

Exercise: Overload max so it works for two ints and for threeints. Print both results from main.

Show solution
public class Main {
  static int max(int a, int b) {
    return a > b ? a : b;
  }

  static int max(int a, int b, int c) {
    return max(max(a, b), c);
  }

  public static void main(String[] args) {
    System.out.println(max(3, 7));        // 7
    System.out.println(max(3, 7, 5));     // 7
  }
}

The three-argument version reuses the two-argument one — a common overload pattern.

Key Takeaways

  • Overloads share a name but differ in parameter number or types.
  • The compiler picks the match from the arguments at the call site.
  • Return type alone cannot distinguish two methods.
  • Overloading is compile-time; overriding (later) is runtime.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Maths

Area for different shapes

Same method name, different parameter lists. A square needs one side; a rectangle needs two. Overloading keeps both under area.

A = s² or A = w × h

Example

public class Main {
  static int area(int side) { return side * side; }
  static int area(int w, int h) { return w * h; }

  public static void main(String[] args) {
    System.out.println(area(4));
    System.out.println(area(3, 5));
  }
}

FAQ: Java Method Overloading

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java method overloading examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java method overloading in this Java Java lesson (Java Method Overloading).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.