codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java method overloading

Java method overloading

Same name, different parameters.

You can write several methods with the same name as long as their parameter lists differ in number or type. Java picks the best match when you call it.

Example code

public class Main {
  static int sum(int a, int b) { return a + b; }
  static double sum(double a, double b) { return a + b; }
  static int sum(int a, int b, int c) { return a + b + c; }

  public static void main(String[] args) {
    System.out.println(sum(1, 2));
    System.out.println(sum(1.5, 2.5));
    System.out.println(sum(1, 2, 3));
  }
}

Run this code in the online editor

← Java methodsJava classes and objects →