×

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep in mind in this situation is that pass-by-value is used when passing a primitive type to a method. Java enables pass by value; pass by reference cannot be used to directly provide primitive types to a method.

However, pass-by-reference is always used to implicitly pass all non-primitive types, which include objects of any class.

Passing a variable by value essentially means sending its actual value, whereas passing a variable by reference essentially means sending the memory location of the variable where it is stored.

Refer the following example to understand how pass-by-reference work:

PassByReference.java

class PassByReference {
     /* we are creating a class named passByReference through which we access the
      * objects of this class
      * we pass the ojects with the help of this class name to the instance
      * methods of the same class.
      * this means with the passed object reference we access the instance 
      * variables of this class
      */
    int a = 10;
    void refer(PassByReference pbr) {
        pbr.a = pbr.a+10; // accessing or updating instance variable with object 
                          // reference
    }
    public static void main(String[] args) {
        PassByReference pbr = new PassByReference();
        System.out.println("Before pass-by-reference: " + pbr.a);
        //value before upadating 
        pbr.refer(pbr);
        System.out.println("After pass-by-reference: " + pbr.a);
         //after updating through object reference
    }
}

Output:

Java Pass-by-Reference

Generally, primitive data types are passed by value, but they can be accessed through object reference.

This can be done in three ways:

  • Inside a class, make the member variable public.
  • Return a value from a method and update the same inside the class.
  • Make a single element array and send it to the method as a parameter.

Mistakes to be rectified with object reference:

  • attempting to reference-modify an immutable value.
  • attempting to reference-modify a primitive variable.
  • When you modify a mutable object argument in a method, the expected behaviour of the real object remains unchanged.

Points to be noted:

  • Java always sends value-based parameter variables.
  • Java object variables always refer to the actual object's memory heap location.
  • When an object is passed to a method, its value can be altered.
  • Even if a new value is supplied to an immutable object, its value cannot be altered.
  • Passing a copy of the item is referred to as "passing by value."
  • Passing a variable's actual memory reference is referred to as "passing by reference."

Related Topics

Pattern Programs in Java

Pattern Programs in Java In Java, pattern programs are the most important from the perspective of interviews. The pattern programs improve thinking and coding skills. It also helps us to develop a...

27 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Java Identifiers

In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many...

3 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.