Is Java “Pass-by-Value” or “Pass-by-Reference” ?

Let us start from C Language. C supports “Pass-by-Value” and “Pass-by-Pointers”. C++ supports “Pass-by-value”, “Pass-by-reference” and “Pass-by-Pointers”. In Java, there is only “Pass-by-Value” – this is what the Java Specification says.

“Passing” is nothing but sending the parameters from a method call to a method definition. As a simple example, in the below program :

class Dog


        public int a;

        public void Display( )


                System.out.println( a );



public class PassingTest


        public static void main( String [] args )


                Dog d1 = new Dog( );


                d.Display( );


        public static void Fun( Dog d2 )


                d.a = 6;





These terms are associated with method calling and passing variables as method parameters. Well, primitive types are always pass by value without any confusion.

The reference variable contains a kind of address (that java can understand) to locate the actual object in the heap. When we pass a reference type ( i.e. Dog type in our below example ) as a method parameter, always the memory address  is copied to new reference variable ( i.e. d1 to d2 in our below example ) bit by bit. See the below program. So what gets copied from ‘d1’ to ‘d2’ is the value – the bit information used to locate the actual object on the heap.

In above example, address bits of first instance are copied to another reference variable, thus resulting both references to point a single memory location where actual object is stored. This is proved by printing the value of ‘a’. One more thing to remember is that, making another reference ‘d2’ to null will not make first reference ‘d1’ also null. But, changing state from either reference variable have impact seen in other reference also. The Java Spec says that everything in Java is pass-by-value. There is no such thing as “pass-by-reference” in Java.

About the Author:

Subhash.K.U, Principal Mentor,

Subhash Programming Classes, Bangalore

View My Complete Profile !

I love to teach you ! Click here !

My name is Subhash.K.U. I work as Principal Mentor at Subhash Programming Classes helping fresh engineering graduates and professionals crack their programming interviews through C, C++, Data Structures, Java and Linux Programming. 

No Comment

You can post first response comment.

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.