Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Java, you cannot define default parameter values for method parameters like you can in some other programming languages. However, there are a few different approaches you can take to achieve similar functionality.

One approach is to overload the method. For example, you could define two versions of the calculate() method, one with one parameter and one with two parameters:

public int calculate(int x) {
    return calculate(x, 2);
}

public int calculate(int x, int y) {
    // your calculation logic here
}