Passing Strings By Reference in Java10 May 2025 | 3 min read In Java, primitive data type variables like int, char, and float are passed by value. It indicates that a duplicate of the variable's value is sent to a method or function. Nonetheless, regarding the passing of objects such as Strings, the distinction between passing by reference and passing by value may occasionally be perplexing. Understanding Strings as Objects in JavaStrings in Java are objects that denote sequences of characters. They are formed using the String class, and their unchangeability is an essential trait. When handling Strings, any action that seems to alter them actually generates a new String object. Understanding Passing by Reference vs Passing by ValueWhen a variable of a primitive data type is provided as an argument to a method, a duplicate of the variable's value is sent. Modifications made to the parameter within the method do not influence the original variable beyond the method. Output: After method call: Hello, World! Passing by ReferenceWhen an object is sent as a parameter to a method, the reference to the object's memory address is transmitted rather than a duplicate of the object itself. It indicates that modifications to the object inside the method can be shown in the original object. Nonetheless, in Java, objects are also passed by value; however, it's the value of the reference (the memory address) that gets passed. Output: After method call: 20 Can we pass string by reference in Java?Strings in Java are unchangeable, which implies that their content cannot be altered after they have been created. When a String is sent to a method, a duplicate of the reference to the String is sent. Nonetheless, because Strings are immutable, any action that seems to alter the String actually produces a new String object, keeping the original one intact. Let’s examine this through an illustration: Output: Inside method: World After method call: Hello Workaround to Pass Strings by "Reference"Although Strings are immutable and cannot be directly passed by reference, you can use a wrapper object to achieve similar behaviour. By encapsulating the String inside a mutable object, you can indirectly modify the String. For example: Output: After method call: Hello World Modifying Strings IndirectlyWhile Strings cannot be modified directly due to their immutability, you can use mutable objects such as StringBuilder or StringBuffer to achieve similar functionality. Here is an example: Output: Inside method: Hello World After method call: Hello World Next TopicGroovy vs Java |
Types of Interfaces in Java
In Java interfaces are essential, when it comes to setting class contracts and guaranteeing code consistency in the realm of Java programming. It serves as a class's blueprint by outlining a number of methods that the implementing class is required to implement. The abstraction, modularity, and...
4 min read
Replace Element in Arraylist Java
The ArrayList data structure in Java allows programmers to store and manage components in a resizable array dynamically and flexibly. In this section, we will discuss various methods for replacing elements in an ArrayList in Java. Developers will be equipped with adaptable solutions through the presentation...
5 min read
Type Safety and Type Casting in Java Generics
All variables and expressions in Java use static typing during compilation. Each element and expression get linked to a specific data type when developers run the code compilation process. The static typing feature of the language protects operations while ensuring users perform methods on compatible data types...
5 min read
How to convert String to String array in Java
How to Convert a String to String Array in Java? In Java, a String is an object that represents the sequence of characters. In order to use Strings, we need to import the String class defined in java.lang package. The String array is an array of strings...
6 min read
JVM Shutdown Hook in Java
Developers can insert a piece of code to run when the JVM is shutting down using a specific construct called a shutdown hook. It is useful when we need to do certain cleanup procedures in the event that the JVM is shutting down. When the VM is...
4 min read
Heap implementation in Java
Heap Implementation in Java In Java, Heap is a special type of data structure where the root node or parent node is compared with its left and right children and arranged according to the order. Suppose, x is a root node and y is the child...
21 min read
Half Diamond Pattern in Java
Programming is not just about solving complex problems or creating functional software; it is also an art form. One way to explore the artistic side of programming is by creating beautiful patterns and designs using code. In this section, we will delve into the fascinating world...
5 min read
Arithmetic Exception in Java
The Exception Handling is one of the most powerful mechanisms to handle the runtime errors so that the normal flow of the application can be maintained. In Java, exception is an abnormal condition. Java programming language defines various exceptions. In this section, we will discuss the...
3 min read
Check If the Given Two Matrices Are Mirror Images of One Another in Java
In Java programming, determining if two matrices are mirror images of each other involves comparing corresponding elements in reverse order. A matrix is considered a mirror image of another if its rows or columns are reversed versions of the corresponding rows or columns in the...
6 min read
Java Thread Dump Analyser
Java thread dumps are a helpful tool for diagnosing performance issues and troubleshooting Java application issues. But for individuals who are not familiar with thread dump analysis, reading and comprehending these thread dumps might be a difficult undertaking. A Java Thread Dump Analyzer is useful in...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India