Javatpoint Logo
Javatpoint Logo

Parameter Passing Techniques in Java with Examples

Parameter passing in Java refers to the mechanism of transferring data between methods or functions. Java supports two types of parameters passing techniques

  1. Call-by-value
  2. Call-by-reference.

Understanding these techniques is essential for effectively utilizing method parameters in Java.

Parameter Passing Techniques in Java with Examples

Types of Parameters:

1. Formal Parameter:

A variable and its corresponding data type are referred to as formal parameters when they exist in the definition or prototype of a function or method. As soon as the function or method is called and it serves as a placeholder for an argument that will be supplied. The function or method performs calculations or actions using the formal parameter.

Syntax:

In the above syntax:

  • returnType represents the return type of the function.
  • functionName represents the name of the function.
  • dataType represents the data type of the formal parameter.
  • parameterName represents the name of the formal parameter.

2. Actual Parameter:

The value or expression that corresponds to a formal parameter and is supplied to a function or method during a function or method call is referred to as an actual parameter is also known as an argument. It offers the real information or value that the method or function will work with.

Syntax:

functionName(argument)

In the above syntax:

  • functionName represents the name of the function or method.
  • argument represents the actual value or expression being passed as an argument to the function or method.

1. Call-by-Value:

In Call-by-value the copy of the value of the actual parameter is passed to the formal parameter of the method. Any of the modifications made to the formal parameter within the method do not affect the actual parameter.

ALGORITHM:

Step 1: Create a class named CallByValueExample.

Step 2: Inside the main method:

Step 2.1: Declare an integer variable num and assign it the value 10.

Step 2.2: Print the value of num before calling the method.

Step 2.3: Call the modifyValue method, passing num as the actual parameter.

Step 2.4: Print the value of num after calling the method.

Step 3: Define the modifyValue method that takes an integer parameter value:

Step 3.1: Modify the formal parameter value by assigning it the value 20.

Step 3.2: Print the value of value inside the method.

Implementation:

The implementation of the above steps given below

FileName: CallByValueExample.java

Output:

Before calling method: 10
Inside method: 20
After calling method: 10

Complexity Analysis:

Time Complexity is O(1).

Space Complexity is O(1).

Call-by-Reference:

call by reference" is a method of passing arguments to functions or methods where the memory address (or reference) of the variable is passed rather than the value itself. This means that changes made to the formal parameter within the function affect the actual parameter in the calling environment.

In "call by reference," when a reference to a variable is passed, any modifications made to the parameter inside the function are transmitted back to the caller. This is because the formal parameter receives a reference (or pointer) to the actual data.

ALGORITHM:

Step 1: Start

Step 2: Define the class "CallByReference"

Step 2.1: Declare instance variables: a (int) and b (int)

Step 2.1: Define a constructor to assign values to a and b

Step 3: Define the method "changeValue" inside the "CallByReference" class:

Step 3.1: Accept a parameter of type "CallByReference" called "obj"

Step 3.2: Add 10 to the value of "obj.a"

Step 3.3: Add 20 to the value of "obj.b"

Step 4: Define the class "Main"

Step 4.1: Define the main method

Step 4.2: Create an instance of "CallByReference" called "object" with values 10 and 20

Step 4.3: Print the values of "object.a" and "object.b"

Step 4.4: Call the "changeValue" method on "object" and pass "object" as an argument

Step 4.5: Print the updated values of "object.a" and "object.b"

Step 5: End

Implementation:

The implementation of the above steps given below

FileName: CallByReferenceExample.java

Output:

Value of a: 10 & b: 20
Value of a: 20 & b: 40

Complexity Analysis:

Time Complexity is O(1).

Space Complexity is O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA