Function Multiple arguments in Java 20

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: Java 20

In Java, a method or function can have multiple arguments, which enables you to pass more than one value to the method. To use multiple arguments in a method or function, you need to specify the data type of each argument and separate them using commas. Here is an example that demonstrates how to use multiple arguments in a method:

public static void printFullName(String firstName, String lastName) {
    System.out.println(firstName + " " + lastName);
}

public static void main(String[] args) {
String first = "John";
String last = "Doe";
printFullName(first, last);
}

In the above example, the printFullName() method takes two arguments of type String named firstName and lastName. This method concatenates the first name and last name and prints the full name to the console. In the main() method, we define two string variables named first and last and pass them as arguments to the printFullName() method.

You can also use different data types for multiple arguments, such as int, double, boolean, etc. Here is another example that demonstrates this:

public static int sum(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
int x = 5;
int y = 10;
int result = sum(x, y);
System.out.println("The sum of " + x + " and " + y + " is " + result);
}

In this example, the sum() method takes two integer arguments a and b and returns their sum. In the main() method, we define two integer variables named x and y with values of 5 and 10, respectively. We then call the sum() method with these variables as arguments and store the result in the variable result. Finally, we print the result to the console using a println() method with a concatenated string.