Parameters In Java Methods
The Java methods parameters are currently under study, in order to satisfy readers’ thirst for knowledge with examples.
Java Methods Parameters and Arguments
Java methods parameters are information that can be passed to methods as parameters.
It is the parameters that act as variables within the method.
After a method’s name, parameters are specified inside parentheses.
Parameters can be added as many times as you wish, just separate them by commas.
In the following example, a String named my_Name is passed as a parameter to a method.
A my_Name is passed along when the method is called, and it is used within the method to print the full name:
public class Main {static void parameterized_Method(String my_Name) {
System.out.println("Hey! my name is : " + my_Name);
}public static void main(String[] args) {parameterized_Method("Elon Musk");
parameterized_Method("Jeff Bezos ");
parameterized_Method("Albert Einstein");}// Output1 = Hey! my name is : Elon Musk
// Output2 = Hey! my name is : Jeff Bezos
// Output3= Hey! my name is : Albert Einstein
}
An argument is a parameter passed to a method. With respect to Java methods, my_Name represents a parameter, while Elon Musk, Jeff Bezos, and Albert Einstein represent arguments as per above example.
public class Main {static void parameterized_Method_Int(int mrx_Age) {
System.out.println("Your age is : " + mrx_Age);
}public static void main(String[] args) {parameterized_Method_Int(22);
parameterized_Method_Int(43);
parameterized_Method_Int(61);}// Output1 = Your age is : 22
// Output2 = Your age is : 43
// Output3= Your age is : 61
}
Multiple Parameters
There are no limits to how many parameters you can have:
public class Main {static void dual_parameterized_Method(String my_Name,int age){
System.out.println("Hey! my name is: "+my_Name+" and my age is: "+age);
}public static void main(String[] args) {dual_parameterized_Method("Elon Musk",51);
dual_parameterized_Method("Jeff Bezos",58);
dual_parameterized_Method("Albert Einstein",76);}// Output1 = Hey my name is: Elon Musk and my age is: 51
// Output2 = Hey my name is: Jeff Bezos and my age is: 58
// Output3 = Hey my name is: Albert Einstein and my age is: 76
}
public class Main {static void multiple_parameters_Method(String mrx_name,int ample_Age,String mr_country,float height_in_feet){
System.out.println("Username is: "+mrx_name+" Age is: "+ample_Age+" Country = "+mr_country+" Height: "+height_in_feet+" feet");
}public static void main(String[] args) {multiple_parameters_Method("Charles",24,"Berlin",5.11f);
multiple_parameters_Method("Robert",27,"Finland",6.2f);
multiple_parameters_Method("Caroline",31,"Brazil",5.4f);
multiple_parameters_Method("Jennica",19,"Australia",5.5f);}// Output1 = Username is: Charles Age is: 24 Country = Berlin Height: 5.11 feet
// Output2 = Username is: Robert Age is: 27 Country = Finland Height: 6.2 feet
// Output3 = Username is: Caroline Age is: 31 Country = Brazil Height: 5.4 feet
// Output4 = Username is: Jennica Age is: 19 Country = Australia Height: 5.5 feet}
It is important to note that when you are dealing with multiple parameters, you must pass the same number of arguments as there are parameters, and the arguments must be passed in the same order.
Return Values
In the examples above, the void keyword indicates that the method should not return a value.
It is possible to return a value by using a primitive data type (such as int, char, etc.) instead of void, and using the return keyword in the method:
public class Main {static int returned_Method(int mrx){return(20+mrx);}public static void main(String[] args) {System.out.println("Sum: "+returned_Method(15));}
}
The following example returns the product of 13 and 10:
public class Main {static int second_return_method(int ample){return (10*ample);}public static void main(String[] args) {System.out.println("After multiplying the result is: "+second_return_method(13));}
}
As an example of Java method parameters, this example returns the product of a method’s two parameters:
public class Main {static int returned_Method(int mrx,int ample){return(mrx*ample);}public static void main(String[] args) {System.out.println("Result: "+returned_Method(7,5));}// Output Result = 35 (7 x 5)}
For example, the following Java method returns the sum of two parameters(int mrx,int ample) of a method:
public class Main {static int sum_return_method(int mrx,int ample){return (mrx+ample);}public static void main(String[] args) {System.out.println("After Adding both integers the result is: "+sum_return_method(11,22));}// Output: After Adding both integers the result is: 33
}
In addition, you can store the result in a variable (which is recommended, as it is easier to read and maintain):
public class Main {static int returned_Method(int mrx){return (5*mrx);}public static void main(String[] args) {int result= returned_Method(6);System.out.println("Result: "+result);}
// Output Result : 30 (5 * 6)
}
public class Main {static int sum_return_method(int ample,int mrx){return (ample+mrx);}public static void main(String[] args) {int sum=sum_return_method(3,61); // Storing the sum value in variable sumSystem.out.println("The sum of two integers is: "+sum);}// Output: The sum of two integers is: 64
}
Method with If-Else Statements
Whenever we discuss Java methods parameters, we often use if-else statements:
public class Main {static void voter_eligibility_system(int age){/* Create a program that checks the age of a voter and tells if he is or not eligible to cast the vote */// If the voter is under-age he cannot cast voteif(age < 18){
System.out.println("Sorry! You are not eligibile to cast vote ");
}// The voter can cast a vote if the age is over 18 or equal to 18.else{
System.out.println("Congratulations! You are eligible to cast vote");
}
}public static void main(String[] args) {voter_eligibility_system(22);//The voter_eligibility_system method is called and the age(22) is passed as an argument}// Output = "Congratulations! You are eligible to cast vote"
}
The following method takes person_height as a parameter and shows if a person is dwarf or not.
public class Main {static void dwarf_analyzer(float person_height){if(person_height >= 0 & person_height <= 4.10){
System.out.println("Person is a Dwarf");
}
else{
System.out.println("Person is not a Dwarf");
}
}public static void main(String[] args) {dwarf_analyzer(4.08f);
}
}