- Java Parameter Explained [Easy Examples]
- Getting Started with Java Parameters
- Difference between Java Parameters and Java Arguments
- Syntax of Java parameters
- Java parameters data type
- Example-1 User-defined function and integer typed Java parameters
- Example-2 User-defined function and String typed Java parameters
- Example-3 User-defined function and double typed Java parameters
- Example-4 Passing Object as a parameter in a function
- Summary
- Further Reading
- Leave a Comment Cancel reply
- Java Tutorial
- Java Method Parameters
- Example
- Multiple Parameters
- Example
- Return Values
- Example
- Example
- Example
- A Method with If. Else
- Example
Java Parameter Explained [Easy Examples]
A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. In this tutorial, we will discuss java parameters. We will discuss how we can declare java parameters by taking various examples. We will see how we can declare different data typed parameters in java including integers, strings, and floats, and will solve different examples. We will also discuss the common difference between java parameters and java arguments. All in all, this tutorial is going to have all the necessary information and examples that you need to learn in order to start and understand the java parameters.
Getting Started with Java Parameters
Java parameters are variable names with type that is declared within the method signature. The list of parameters is enclosed in parenthesis and each parameter consists of two parts: type name followed by the variable name. Java parameters declared in the method signature are always local variables that receive values when the method is called from another method. In this section, we will discuss the difference between java parameters and arguments and will also solve various examples and learn how we declare different types of parameters.
Difference between Java Parameters and Java Arguments
Arguments in Java are the actual values that are passed to variables defined in the method header when the method is called from another method. That is, whenever any particular method is called during the execution of the program, there are some values that are passed to call that particular method. You can read more about java arguments from the article on Java arguments. Here we will just highlight the common differences between java parameters and java arguments. See the list below:
- When a function is called, the values that are passed in the call are called arguments. While the variables that are written at the time of the function prototype and the definition of the function are called java parameters.
- Java arguments are used in the function call statement to send values from the calling function to the called function while Java parameters are used in the function header of the called function to receive the value from the arguments.
- During the time of call, each argument is always assigned to the parameter in the function definition. While parameters are local variables that are assigned a value of the arguments when the function is called.
- Java arguments are also known as actual parameters. While the parameters are also known as formal parameters.
Syntax of Java parameters
Now we already have sufficient knowledge of java parameters. Let us know start practically implementing the knowledge. Here, we will see the basic syntax of Java parameters declaration in the function. See the simple syntax below:
Function Name(Datatype variableName, Datatype VariableName, . );
Notice there are two things that are important while declaring java parameters. First, we have to mention the data type of the parameter which is followed by the name of the variable. The declared variable (parameters) can only accept the values of the specified data type, otherwise, it will return an error.
Java parameters data type
We can use any data type such as primitive data types including int, float, double, char, short, byte, String, and object reference variables for a parameter of a method and constructor. There is no standard limit to specify the number of parameters in the definition of a method. In this section, we will solve examples of integer, double, string, and object reference variables for java parameters.
Example-1 User-defined function and integer typed Java parameters
Now let us practically implement and create java parameters. We will first create a user-defined function which only accepts integer typed variables. See the example below:
// Java main class class Main < // java user defined function with java parameters public static Integer sum(Integer num1, Integer num2)< // return the sum of the parameters return num1+num2; >>
Notice that in the user-defined function, we had declared two integers typed parameters. And the function returns the sum of the two numbers. To execute this function, we have to call this function from the java main method. See the example below which call the above function:
// Java main class class Main < // java user defined function with java parameters public static Integer sum(Integer num1, Integer num2)< // return the sum of the parameters return num1+num2; >// java main method public static void main(String[] args) < // calling the user defined function System.out.println("The sum is: "+ sum(2, 3)); >>
Notice that we get the sum of the two arguments that we provided to our sum function.
Example-2 User-defined function and String typed Java parameters
We already had seen how we declare java integer types parameters. Let us now take an example and see how we can declare string typed java parameters in a user-defined function. First, we will create a user-defined function that can take strings as parameters and will return the string values. See the example below:
// Java main class class Main < // java user defined function with java parameters public static String stringfun(String value1, String value2)< // return the parameters return value1+value2; >>
Notice that we have declared the type of parameters as a string. We need to call this method in order to executed by providing the required arguments. See the code below, which calls this function:
// Java main class class Main < // java user defined function with java parameters public static String stringfun(String value1, String value2)< // return the parameters return value1+value2; >// java main method public static void main(String[] args) < // calling the user defined function System.out.println(stringfun("Go", "LinuxCloud")); >>
Notice that we provided the required string typed arguments while calling the user-defined function.
Example-3 User-defined function and double typed Java parameters
Now let us take an example and see how we can declare double data typed parameters in java. See the example below:
// Java main class class Main < // java user defined function with java parameters public static Double doubleSum(Double value1, Double value2)< // return the parameters return value1+value2; >>
Notice that we have defined the type of the variables in our user-defined function. We can call the function from the java main method to execute the code. See the example program below:
// Java main class class Main < // java user defined function with java parameters public static Double floatSum(Double value1, Double value2)< // return the parameters return value1+value2; >// java main method public static void main(String[] args) < // calling the user defined function System.out.println(floatSum(3.4, 5.5)); >>
Notice that we get the sum of the two double numbers that we have used as arguments while calling the function.
Example-4 Passing Object as a parameter in a function
In Java, When a primitive type is passed to a method, it is done by use of call-by-value. Objects are implicitly passed by the use of call-by-reference. This means when we pass primitive data types to method it will pass only values to function parameters so any change made in parameter will not affect the value of actual parameters. Whereas Objects in java are reference variables, so for objects a value which is the reference to the object is passed. Hence the whole object is not passed but its reference gets passed. All modifications to the object in the method would modify the object in the Heap. See the example below:
// square class class Square < // varible int a; // parametrized constructor Square(int x)< a=x; >// object Square passed as parameter in function square void square(Square num) < // finding the square int sq = num.a*num.a; // printing the square System.out.println("Square of the number is :"+sq); >> // java main class public class Main < // java main method public static void main(String arg[])< // creating new object of type Square Square number=new Square(3); // Calling the parametrized constructor with a parameter number.square(number); >>
Square of the number is :9
Notice that we created a new object in our main class of type Square and then call the square method which actually takes the object as a parameter.
Summary
A parameter is a variable in the definition of a method also known as a formal parameter. In this tutorial, we learned about java parameters. We covered the common differences between java parameters and java arguments which many people mix. We also learned the basic syntax to declare a function with java parameters and then we practically created various data typed parameters by taking different examples. For instance, we created Object type, integer, double, and string typed parameters using the java programming language. All in all, this tutorial, covers all the necessary information and examples that you need to know in order to start working and understanding java parameters.
Further Reading
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Leave a Comment Cancel reply
Java Tutorial
- Set Up Java Environment
- Set Up Java on Linux
- Set up Java with BlueJ IDE
- Set up Java with VSC IDE
- Set up Java with Eclipse IDE
- Java Multiline Comments
- Java Variables
- Java Global Variables
- Java Date & Time Format
- Different Java Data Types
- Java Booleans
- Java Strings
- Java Array
- Java Byte
- Java convert list to map
- Java convert double to string
- Java convert String to Date
- Java convert Set to List
- Java convert char to int
- Java convert long to string
- Java Operators Introduction
- Java Boolean Operators
- Java Relational Operators
- Java Arithmetic Operators
- Java Bitwise Operators
- Java Unary Operators
- Java Logical Operators
- Java XOR (^) Operator
- Java Switch Statement
- Java If Else Statement
- Java While Loop
- Java For / For Each Loop
- Java Break Continue
- Java Nested Loops
- Java throw exception
- Java Try Catch
- Java Accessor and Mutator Methods
- Java main() Method
- IndexOf() Java Method
- Java ListIterator() Method
- Java create & write to file
- Java read file
- Java Parameter
- Java Argument
- Java Optional Parameters
- Java Arguments vs Parameters
- Java Arrays.asList
- Java HashSet
- Java Math
- Java HashMap vs Hashtable vs HashSet
- Java LinkedList
- Linked List Cycle
- Java List vs LinkedList
- Java ArrayList vs LinkedList
Java Method Parameters
Information can be passed to methods as parameter. Parameters act as variables inside the method.
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:
Example
public class Main < static void myMethod(String fname) < System.out.println(fname + " Refsnes"); >public static void main(String[] args) < myMethod("Liam"); myMethod("Jenny"); myMethod("Anja"); >> // Liam Refsnes // Jenny Refsnes // Anja Refsnes
When a parameter is passed to the method, it is called an argument. So, from the example above: fname is a parameter, while Liam , Jenny and Anja are arguments.
Multiple Parameters
You can have as many parameters as you like:
Example
public class Main < static void myMethod(String fname, int age) < System.out.println(fname + " is " + age); >public static void main(String[] args) < myMethod("Liam", 5); myMethod("Jenny", 8); myMethod("Anja", 31); >> // Liam is 5 // Jenny is 8 // Anja is 31
Note that when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
Return Values
The void keyword, used in the examples above, indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type (such as int , char , etc.) instead of void , and use the return keyword inside the method:
Example
public class Main < static int myMethod(int x) < return 5 + x; > public static void main(String[] args) < System.out.println(myMethod(3)); >> // Outputs 8 (5 + 3)
This example returns the sum of a method’s two parameters:
Example
public class Main < static int myMethod(int x, int y) < return x + y; >public static void main(String[] args) < System.out.println(myMethod(5, 3)); >> // Outputs 8 (5 + 3)
You can also store the result in a variable (recommended, as it is easier to read and maintain):
Example
public class Main < static int myMethod(int x, int y) < return x + y; >public static void main(String[] args) < int z = myMethod(5, 3); System.out.println(z); >> // Outputs 8 (5 + 3)
A Method with If. Else
It is common to use if. else statements inside methods:
Example
public class Main < // Create a checkAge() method with an integer variable called age static void checkAge(int age) < // If age is less than 18, print "access denied" if (age < 18) < System.out.println("Access denied - You are not old enough!"); // If age is greater than, or equal to, 18, print "access granted" >else < System.out.println("Access granted - You are old enough!"); >> public static void main(String[] args) < checkAge(20); // Call the checkAge method and pass along an age of 20 >> // Outputs "Access granted - You are old enough!"