Avatar ·

How to use optional parameters in Java? (analog of default arguments)

Hi everyone. I'm writing in Java and ran into a problem. In other languages, like Python or JavaScript, I can set default parameter values directly in the method signature. For example: `def connect(host, port=8080)`. This is very convenient — you can call the method with one parameter, and the second one is automatically substituted. In Java, I'm trying to do something similar, but the compiler complains. Here's my code: ```java public class DatabaseConnector { public void connect(String host, int port = 8080) { // Error! System.out.println("Connecting to " + host + ":" + port); } } ``` Java does not support default values for parameters. I have to write overloaded methods, but this becomes cumbersome, especially if there are many parameters (e.g., host, port, username, password).