How do I use optional parameters in Java?

In Java, you can use method overloading to achieve the effect of optional parameters. Method overloading is the practice of having multiple methods with the same name but different parameter lists.

For example, consider the following class:

public class MyClass {
  public void doSomething(int x) {
    // do something with x
  }

  public void doSomething(int x, int y) {
    // do something with x and y
  }

  public void doSomething(int x, int y, int z) {
    // do something with x, y, and z
  }
}

In this example, the doSomething method has three overloads. The first overload takes a single integer argument, the second overload takes two integer arguments, and the third overload takes three integer arguments.

You can call these methods like this:

MyClass c = new MyClass();
c.doSomething(1);      // calls the first overload
c.doSomething(1, 2);   // calls the second overload
c.doSomething(1, 2, 3); // calls the third overload

By using method overloading, you can provide default values for optional parameters by including them in the method overloads that take fewer arguments.

Note that Java does not support default parameter values like some other languages do.