How are parameters passed in Java?


The most frequent query asked by beginner programmers is that how are parameters passed in Java. Generally, the programming languages use pass by value and pass byreference for passing parameters to a method. However, Java does not support both approaches rather it uses pass by value to pass both primitive and reference type values. In this article, we are going to understand passing parameters by value through exampleprograms.

Passing Parameters to a Method in Java

Let’s start this discussion by understanding the storage mechanism of Java. The referencevariables, names of methods and classes are stored in stack and their values are storedin heap. But, the primitives are stored directly in the stack memory along with their values.

As explained earlier, Java supports only pass-by-value for both primitive and reference types which means when a method is invoked, a copy of the value of each parameter is passed to that method

For primitive types such as int, double and Boolean the value of the parameter is same asthe original value of the variable. For example, if we have a variable ‘x’ with a value of 10,and we pass ‘x’ as a parameter to a method, then the method receives a copy of originalvalue 10 as its parameter.

Since reference variables are stored in stack, therefore, for reference types such as arrays,objects and strings the value of the parameter is the reference or address of the givenvariable. For example, if we have an array ‘arr’ with elements {1, 2, 3} and we pass ‘arr’as a parameter to a method, then the method receives the copy of reference or addressof ‘arr’ as its parameter.

Let’s discuss some terms related to parameter passing in Java

User-defined Method

Methods are blocks of code that can be reused multiple times to perform a single operation.It saves our time and also reduces the size of code.

Syntax

accessSpecifier nonAccessModifier return_Type method_Name(Parameters) {
   // Body of the method
}

Here,

accessSpecifier − It is used to set the accessibility of the method. It may be public, protected, default and private.

nonAccessModifier − It shows additional functionalities or behavior of the method such as static and final.

return_Type − The datatype a method is going to return. We use the void keyword when method does not return anything.

Parameters vs Arguments

Parameters − are the named variable passed during the method definition and serve as a placeholder for arguments. They actually import the arguments into the specified method. They allow a method to be generalized. Here, generalized means we can reuse a single method with various data according to the needs.

Arguments − are the actual values that are passed during the time of method call. They must match the type of parameter passed in the method definition.

Pass by Value

In this way of passing parameters, a copy of parameter value is passed to the given method. That method can modify the copy, but it cannot affect the original value of the parameter

Pass by Reference

In this way of passing parameters, a reference or an address of the parameter is passed to the given method. That method can modify the original value of the parameter by using the reference

Java Programs to show Passing of Parameters

Approach

  • Define two user-defined methods. The ‘changeVal()’ method to modify primitive type and ‘changeAray()’ to modify reference type.

  • In the main() method, declare and initialize an integer type value and a referencetype value.

  • Now, call those user-defined methods to perform operation and exit

Example

import java.util.*;
public class ParameterPassing {
   public static void changeVal(int n) {
      n = n * 2; // modifying value
      System.out.println("Inside method: n = " + n);
   }
   public static void changeAray(int[] a) {
      a[0] = a[0] * 2; // modifying value
      System.out.println("Inside method: a = " + Arrays.toString(a));
   }
   public static void main(String[] args) {
      // for the Primitive type
      int val1 = 10;
      System.out.println("Before calling changeVal method: val1 = " + val1);
      changeVal(val1); // calling the method
      System.out.println("After calling changeVal method: val1 = " + val1);
      
      // for Reference type
      int[] aray = {1, 2, 3};
      System.out.println("Before calling changeAray method: arr = " + Arrays.toString(aray));
      changeAray(aray); // calling the method
      System.out.println("After calling changeAray method: arr = " + Arrays.toString(aray));
   }
}

Output

Before calling changeVal method: val1 = 10
Inside method: n = 20
After calling changeVal method: val1 = 10
Before calling changeAray method: arr = [1, 2, 3]
Inside method: a = [2, 2, 3]
After calling changeAray method: arr = [2, 2, 3]

As you can see in the output, for primitive type ‘val1’, its value does not change aftercalling ‘changeVal’ method. For reference type ‘arr’, its value does change after calling‘changeAray’ method.

Conclusion

In this article, we have learned user-defined methods and with the help of an example, we discussed how parameters are passed to a method in Java. Passing reference type value as a parameter works differently than primitive types. Java supports pass reference for non-primitive types only in terms of modifying their state, not their identity.

Updated on: 20-Jul-2023

690 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements