In Java, you can set a default value for an argument in a method or function using the assignment operator =. When you set a default value for an argument in a method or function, that argument is optional when calling the method or function, and if it is not provided, the default value is used.
Here are some examples of how to use default value for arguments in a method or function in Java:
Example 1:
public void greet(String name, String greetingMsg = "Hello") {
System.out.println(greetingMsg + ", " + name + "!");
}
In this example, the greet method takes two arguments, name and greetingMsg. The greetingMsg argument has a default value of "Hello". If the greetingMsg argument is not provided when calling the greet method, the default value "Hello" will be used.
Example 2:
public int addNumbers(int num1, int num2, int num3 = 0) {
return num1 + num2 + num3;
}
In this example, the addNumbers method takes three arguments, num1, num2, and num3. The num3 argument has a default value of 0. If the num3 argument is not provided when calling the addNumbers method, the default value 0 will be used.
Example 3:
public void printUserInfo(String name, String email = null, int age = 0) {
System.out.println("Name: " + name);
System.out.println("Email: " + email);
System.out.println("Age: " + age);
}
In this example, the printUserInfo method takes three arguments, name, email, and age. The email argument has a default value of null, which means that it is optional and can be omitted when calling the method. The age argument has a default value of 0, which means that it is also optional and can be omitted when calling the method.
Overall, setting default values for arguments in methods or functions can help make your code more flexible and easier to use.