Pass By Value In Java

In this tutorial, we will learn about how to implement the concept of Pass By Value in Java. Also, we will be looking forward to the concept and look at whether How Java Support  Pass by Value.

If you don’t know how to Pass By Value In Java, then you are in the right place. Because, in this tutorial, we will be Going in deep Understanding of the concept. Also, we will learn about parameters both Formal and actual.

What Actually Is Pass By Value?

Initially, you need to understand that in Java, Parameters are passed by value. Before that, you need to understand what does Formal and Actual Parameters mean.

Formal parameters are the one which is declared in method definition and receieve their values when a call to that method is made. Whereas, Actual Parameters are the ones whom the variables are passed while calling a method.

Now comes Pass by Value. In pass by value, Java works exactly like C. You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, you cannot change where that pointer points.

Actual parameter variables that are passed to a method are evaluated and value is derived. After This, what happens is that the value which we stored, and then that becomes what we observe the formal parameter to that of calling function/method.  This entire mechanism is called pass by value and Java uses this Concept.

Below is code For Explanation of above concept :

class check 
{ 
  public static void main(String[] args) 
  { 
        		int a = 10; 
        		changeIt(a); 
        		System.out.println(a); 
    	} 
    	public static void changeIt(int a) 
    	{ 
        		a = 100; 
    	} 
} 

The above code initializes a variable ‘a’ with 10 and Is passed Into the method ‘changeIt’. After that block, when the display gets worked, The same initial value gets displayed.

After this, we get a conclusion that Java always Passes object reference to the method by Values. This means passing the Memory address of the object that the variable points to at that block/method.

Also read:

Leave a Reply

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