Java Parameter Passing: Pass-by-Reference or Pass-by-Value?

Image of a piece of Java code magnified along with its logo
Java code!

In any programming language, parameter passing is vital for modularizing the source code. Parameter passing allows you to capture values or directly feed them to the source code in the form of parameters. Different programming languages pass parameters in different ways, but pass-by-value and pass-by-reference are the two most prevalent modes.

In this article, I’ll be explaining these two predominant methods for parameter passing. I’ll also show you what method Java uses to pass parameters. Most importantly, I’ll show you how to trick the compiler to get maximum benefits out of Java, the default choice for simplex and complex apps. Let’s jump right in!

Pass-by-Value vs Pass-by-Reference

Pass-by-value and pass-by-reference describe how a language passes variables onto functions. In pass-by-value, a function parameter’s value is copied to another variable. Conversely, in pass-by-reference, you pass/copy the actual parameters to the function. Oftentimes, the code’s security requirements dictate if you should use pass-by-reference or pass-by-value. 

Image explaining the difference between pass-by-reference and pass-by-value by means of animation.
Call by Reference vs Call by Value.

Let’s take a look at each model in detail.

Pass-by-Reference

In most programming languages and the compiler theory, pass-by-reference passes the reference of a parameter being called. In other words, the caller and the callee refer to and use the same variable for the parameter. Whenever this referenced variable changes, all functions that reference this variable also change. 

Pro-Tips

  • Pass-by-reference is viable if you need to call a function that needs to modify its arguments before reusing them
  • Pass-by-reference consumes less memory because it references the same variable as needed

Pass-by-Value

Pass-by-value, on the other hand, passes a copy of a variable as a parameter to the functions. In this method, the value of each variable in the caller is duplicated. It’s also copied into a corresponding new variable of the callee. Whenever the original variable changes, it doesn’t affect the callee. That’s because every callee has a copy of the original parameter instead of the actual parameter itself. 

Pro-Tips

  • Pass-by-value is beneficial when changes to the passed variable shouldn’t affect the actual value
  • Pass-by-value creates a new dummy variable each time, so it consumes more memory

Pass-by-Reference vs Pass-by-Value

To better see the difference between pass-by-reference and pass-by-value, check out this comparison table.

Pass By ReferencePass By Value
It passes the original reference or actual parameters to the functionIt copies the function variable into another dummy variable
Changes made inside the function affect the original variablesChanges made inside the function don’t update the original variables
Requires less memoryUsually requires more memory
Consumes less memory and time than passing-by-valueUsually requires more time to compile and execute due to the copying overhead
Pass by value or pass by reference, what’s the difference?

You’ve now got a clear understanding of the different parameter passing methods, so let’s see which side Java falls on. 

Parameter Passing in Java

In Java, all the primitive variables, like an integer or a character, store the actual values (data). Conversely, non-primitive variables, like a class or an object, store the reference variables containing the addresses of the objects being referred to. 

Static memory allocation and thread execution in Java always use stack memory. On the other hand, Java stores non-primitive variables like objects in Heap storage.

As a rule, Java always passes by value

Java always uses pass-by-value to pass parameters or arguments. Whenever you invoke a method, Java creates a copy of each parameter and passes it to the method. If the parameter you passed is of a primitive data type, this parameter copies the value inside the stack memory. Then, it passes to the called method. However, if the parameter passed is a non-primitive data type as an object, it points a reference or an address to the stack memory where the actual data resides.

Image showcasing Java, a popular OOP language along with its logo.
Java!

In Java, all the non-primitive data types are usually references with the address to the actual data. That’s why, passing an object as a parameter is still technically passing a value, but the value passed is a reference. 

That being said, you can still trick the compiler to pass-by-reference in Java. Let’s see how! 

How to Pass-by-Reference in Java

Java doesn’t support pass-by-reference out-of-the-box, but you can still achieve the functionalities of passing a parameter as a reference. Before that, let me show you why you might want to pass-by-reference in Java.

Java Pass-by-Reference: The Advantages

  • Returns multiple values from a function
  • Changes a parameter’s original value
  • Passes non-primitive data types
  • Consumes less memory
  • Saves copying overhead

If you want to benefit from these advantages, I’ve compiled a list of 3 techniques to pass-by-reference in Java. Basically, you’ll leverage several aspects of Java’s object-oriented programming approach. That way, you can also gain the advantages and benefits of passing by reference to meet your specific needs. Let’s discuss these techniques!

1. Returning a Value and Updating it

This is a rather straightforward technique. You’ll return the changes made to the variables within a function and update their original memory addresses. By defining a method with a return type in Java, you can return the parameter passed in it. That way, you can update the variable’s original value

2. Making a Public Member Variable in a Class

To pass a non-primitive data type as an argument and update multiple member variables, you can pass a class object representing multiple variables of a class to a function. Then, you can also update the public member variables of that object. Through this method, all the changes you make to the passed object’s member variables will become visible in the original memory address. 

3. Creating a Single Element Array

An Array in Java stores multiple homogeneous values in a single variable, so you can create a single element array. Then, you can pass it as a parameter to a function. All the changes you make to the array will also be in the original memory address. That means you can also eliminate the need to create additional variables like in the case of pass-by-value. 

You can also find code snippets for these methods above.

Final Words

In this article, we discussed the two most predominantly employed techniques to pass variables to functions/methods. I showed you the differences between pass-by-reference and pass-by-value, so you can know the benefits of each.  We’ve also looked at how parameter passing happens in Java (reminder: Java uses pass-by-value). Finally, I showed you how you can trick the compiler to achieve the benefits of passing variables by reference in Java although it only strictly supports pass-by-value. These benefits include passing multiple variables at once, changing the parameter’s original value, and consuming less memory.

Have any questions about parameter passing? Check out the FAQ and Resources below! 

FAQ

What is the caller function and callee function?

In the context of a programming language, a caller is a function that calls another function. Conversely, a callee is a function called by the caller function. These functions are essential in referring to variables or objects within a programming language for data passing. For example, in pass-by-reference, the caller and the callee use the same variable as a reference! 

What are primitive and non-primitive variables in Java?

In Java, primitive variables are the predefined variables. By contrast, the non-primitive types are user-defined. Primitive types in Java include boolean, byte, char, short, int, long, float, and double. Non-primitive types in Java include Strings, Arrays, Classes, and Interfaces. In Java, you can pass primitive variables as a value. Conversely, it uses non-primitive variables as reference. That also means Java follows a pass-by-value model.

What Is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a computer programming model based on the concept of ‘objects’ that contains data and code. It also doesn’t focus on functions and logic. Java is one of the most predominantly used and known object-oriented programming languages. Encapsulation, Abstraction, Inheritance, and Polymorphism are the primary OOP principles. 

What is a compiler?

In computing, a compiler is a piece of software code that translates the code written in one programming language into another language. Usually, compilers ensure you’re using the programming language as you should. In Java, you can also trick this compiler to follow a pass-by-reference model, not a pass-by-value. 

What is a Stack Memory?

Stack memory is physical memory in the RAM allocated to each thread at run time. Memory management in Stack follows a last-in-first-out order. It also allows for static memory allocation and the execution of a thread in Java. That also enables Java to strictly follow a pass-by-value model. 

Resources

TechGenix: Newsletters

Subscribe to our newsletters for more quality content.

TechGenix: Article on How to Troubleshoot the Problem Loading Java Applications in IE.

Learn about troubleshooting the problems in loading Java applications in the Internet Explorer here.

Java: Documentation

Learn more about Java and everything you need to know to get started here

Java: Parameter Passing techniques

Learn more about different parameter passing techniques along with code snippets here

TechGenix: New Java Framework Vulnerability

Learn more about the new java framework and its vulnerability here

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top