Java Parameter Explained [Easy Examples]


JAVA

Author: Bashir Alam
Reviewer: Deepak Prasad

 

Introduction to Java Parameters

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));
	}
}

Output:

The sum is: 5

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"));
	}
}

Output:

GoLinuxCloud

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));
	}
}

Output:

8.9

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);
	}
}

Output:

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

Java arguments
Java methods
Java class

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment