Poziv po vrednosti u Javi (Call by Value)

U Javi postoji samo poziv po vrednosti, ne i poziv po referenci. Ako pozovemo metod koji prosleđuje vrednost, to je poznato kao poziv po vrednosti. Promene se dešavaju u pozvanom metodu, ne obuhvataju pozivajući metod.
Primer poziva po vrednosti u Javi
U slučaju poziva po vrednosti izvorna vrednost nije promenjena. Pogledajmo jednostavan primer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Operation{ 
 int data=50
  
 void change(int data){ 
 data=data+100; //promene će biti samo u lokalnim promenljivama
 
     
 public static void main(String args[]){ 
   Operation op=new Operation(); 
  
   System.out.println("before change "+op.data); 
   op.change(500); 
   System.out.println("after change "+op.data); 
  
 
}
Ispis na ekranu:
before change 50
after change 50

 

Primer 2  poziva po vrednosti u Javi
U slučaju poziva po referenci izvorna vrednost se menja ako mi načinimo promene u pozvanom metodu. Ako prosledimo objekat umesto neke primitivne vrednosti, izvorna vrednost će biti promenjena. U ovom primeru prosleđujemo objekat kao vrednost. Pogledajmo jednostavan primer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Operation2{ 
 int data=50
  
 void change(Operation2 op){ 
 op.data=op.data+100; //promene će biti u instancnoj promenljivoj
 }  
    
 public static void main(String args[]){ 
   Operation2 op=new Operation2(); 
  
   System.out.println("before change "+op.data); 
   op.change(op);  //prosleđuje objekt 
   System.out.println("after change "+op.data); 
  
 
}
 
Ispis na ekranu:
before change 50
after change 150

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.

While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter.

In call by value, the modification done to the parameter passed does not reflect in the caller’s scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller’s scope.

Following is the example of the call by value −

The following program shows an example of passing a parameter by value. The values of the arguments remain the same even after the method invocation.

Example – Call By Value

 Live Demo

public class Tester{
   public static void main(String[] args){
      int a = 30;
      int b = 45;
      System.out.println("Before swapping, a = " + a + " and b = " + b);
      // Invoke the swap method
      swapFunction(a, b);
      System.out.println("\n**Now, Before and After swapping values will be same here**:");
      System.out.println("After swapping, a = " + a + " and b is " + b);
   }
   public static void swapFunction(int a, int b) {
      System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
      // Swap n1 with n2
      int c = a;
      a = b;
      b = c;
      System.out.println("After swapping(Inside), a = " + a + " b = " + b);
   }
}

Output

This will produce the following result −

Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45

Example – Call By Reference

Java uses only call by value while passing reference variables as well. It creates a copy of references and passes them as valuable to the methods. As reference points to same address of object, creating a copy of reference is of no harm. But if new object is assigned to reference it will not be reflected.

 Live Demo

public class JavaTester {
   public static void main(String[] args) {
      IntWrapper a = new IntWrapper(30);
      IntWrapper b = new IntWrapper(45);
      System.out.println("Before swapping, a = " + a.a + " and b = " + b.a);
      // Invoke the swap method
      swapFunction(a, b);
      System.out.println("\n**Now, Before and After swapping values will be different here**:");
      System.out.println("After swapping, a = " + a.a + " and b is " + b.a);
   }
   public static void swapFunction(IntWrapper a, IntWrapper b) {
      System.out.println("Before swapping(Inside), a = " + a.a + " b = " + b.a);
      // Swap n1 with n2
      IntWrapper c = new IntWrapper(a.a);
      a.a = b.a;
      b.a = c.a;
      System.out.println("After swapping(Inside), a = " + a.a + " b = " + b.a);
   }
}
class IntWrapper {
   public int a;
   public IntWrapper(int a){ this.a = a;}
}

This will produce the following result −

Output

Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
**Now, Before and After swapping values will be different here**:
After swapping, a = 45 and b is 30

Originally found here: https://www.tutorialspoint.com/Call-by-value-and-Call-by-reference-in-Java

Does Java pass by reference or pass by value? answer

Comments

comments

Show Buttons
Hide Buttons