“Parameters” in Java play a key role in using the code functionalities in a custom and effective way. For instance, these parameters can be associated with a method, constructor, etc to help the developer implement the code logic in a structured form and then evaluate different values multiple times by passing them as a method’s argument.
This helps in sorting the code functionalities, improving the code readability, and making code templates as per the logic.
What are the Method Parameters in Java?
The method parameters act as variables that are specified after the method’s name within the parentheses. These parameters can be used to specify the data type of the required values followed by the defined variables to be used within the method.
What is the Difference Between Parameter and Argument?
A “Parameter” refers to initializing a value of any data type i.e., int, String, etc in which the target value is to be passed. The “Argument”, however, is the value to be passed with respect to the parameter’s defined value while invoking the method.
Demonstration: Differentiating Parameters and Arguments in Java
This example differentiates the parameters and arguments in Java:
package jbArticles;
public class Methodpar {
static void printName(String x) {
System.out.println("Name -> "+x);
}
public static void main(String[] args) {
printName("David");
}}
In this block of code, consider the following steps:
- Define the method “printName()” that contains the given parameter of “String” type.
- In its definition, display the passed name.
- In the “main”, invoke the defined method by passing the name “David” as its argument.
Output
In this output, it can be implied that the passed name as an argument is displayed appropriately via the method.
Example 1: Specifying Single and Multiple Parameters in a Method in Java
In this demonstration, single and multiple parameters can be specified within a method:
package jbArticles;
public class Methodpar {
static void printName(String x) {
System.out.println("Single Parameter Method");
System.out.println("Name -> "+x);
}
static void printDetails(int x, String y) {
System.out.println("\nMultiple Parameters Method");
System.out.println("Age -> "+x);
System.out.println("City -> "+y);
}
public static void main(String[] args) {
printName("David");
printDetails(18, "Los Angeles");
}}
The code explanation is as follows:
- Define the method “printName()” having a single parameter of “String” type that displays it when passed as an argument.
- Now, create another method “printDetails()” comprising two parameters of “int” and “String” types.
- Likewise, in its definition, display the corresponding passed values.
- In “main”, invoke both methods by passing the relevant values as their arguments.
Output
In this output, it can be verified that the passed values in single and multiple-parameterized methods are displayed accordingly.
Method Return Types
Before heading to the next example, overview the return types of the method:
- void: It implies that a method does not return a value.
- int: It retrieves an integer value.
- char: It gets a character type value.
- string: It returns a string value.
Note: Make sure to use the “return” keyword to return the corresponding value when specifying return types.
Example 2: Applying the Method Parameters Based on the Return Values
In this code example, the parameters of the method will be specified according to the method’s return type:
package jbArticles;
public class Methodpar {
static int display(int x) {
return x;
}
public static void main(String[] args) {
System.out.print(display(5));
}}
In this code, create the method “display()” of “int” return type, specify the “int” type parameter and return the passed integer value. After that, in the “main”, invoke the method and pass the given integer value as its argument to be returned.
Output
As seen, the passed integer value is retrieved appropriately.
Likewise, a parameter of any other data type i.e., “String” in this case can also be specified along with the relevant return type, as follows:
Example 3: Applying Mathematical Computations Using the Method Parameters
The below example utilizes the method’s parameters to perform mathematical calculations:
package jbArticles;
public class Methodpar {
static int sum(int x, int y) {
return x + y;
}
static void analyzeId(int x) {
if(x > 18) {
System.out.println("Valid Age");
}
else {
System.out.println("Invalid Age");
}}
public static void main(String[] args) {
System.out.print("Sum -> "+sum(2,3) + "\n"); analyzeId(2);
}}
According to this block of code, consider the following explanation:
- Define the method “sum()” associated with the “int” return type and having the “int” type parameters.
- In its definition, return the sum of the passed “int” values.
- Declare another method “analyzeId()” of the “void” return type to print the output based on the condition applied on the passed integer via the “if/else” statement.
- In “main”, access both the methods by passing the given values. These values will be evaluated in the methods and the corresponding outcome will be returned.
Output
Bonus Example: Creating a Class Constructor With Parameters
The below code demonstration creates a parameterized class constructor instead that is invoked automatically upon creating a class instance:
package jbArticles;
public class Methodpar {
Methodpar(int a, int b){
System.out.println("Multiplication of numbers -> " + a * b);
}
public static void main(String[] args) {
Methodpar object = new Methodpar(2, 3);
}}
The above code’s explanation is as follows:
- In the public “Methodpar” class’s definition, create a parameterized class constructor having two parameters.
- In the constructor’s definition, display the multiplication of the passed values.
- In “main”, create an instance of the class using the “new” keyword and the “Methodpar()” class constructor.
- Also, pass the stated integer values as the constructor’s argument to return their multiplication.
Output
Here, it can be seen that the multiplication of the passed integers is returned appropriately.
Conclusion
The method “parameters” act as variables that are specified after the method’s name within the parentheses. These are used to initialize a value of primitive data types i.e., int, float, etc, and non-primitive or object types like array, String, etc. It is such that the target value is passed as an argument to the respective methods while invoking them(methods).