0% found this document useful (0 votes)
266 views

Call by Value and Call by Reference in Java

Java uses call by value, not call by reference. When a method is called and passed a value, the changes made inside the method do not affect the original value. However, if an object is passed, the changes do affect the original value because the object is passed by reference. The document provides two examples, one showing a primitive value being passed and unchanged, and another showing an object being passed and changed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views

Call by Value and Call by Reference in Java

Java uses call by value, not call by reference. When a method is called and passed a value, the changes made inside the method do not affect the original value. However, if an object is passed, the changes do affect the original value because the object is passed by reference. The document provides two examples, one showing a primitive value being passed and unchanged, and another showing an object being passed and changed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Call by Value and Call by Reference in Java

There is only call by value in java, not call by reference. If we call a method
passing a value, it is known as call by value. The changes being done in the called
method, is not affected in the calling method.

Example of call by value in java

In case of call by value original value is not changed.

class Operation{

int data=50;

void change(int data){

data=data+100;//changes will be in the local variable only

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);

Output:

before change 50
after change 50

Another Example of call by value in java

In case of call by reference original value is changed if we made changes in the


called method. If we pass object in place of any primitive value, original value will
be changed. In this example we are passing object as a value.

Ex:

class Operation2{

int data=50;

void change(Operation2 op){

op.data=op.data+100;//changes will be in the instance variable

public static void main(String args[]){

Operation2 op=new Operation2();

System.out.println("before change "+op.data);

op.change(op);//passing object

System.out.println("after change "+op.data);

Output:

before change 50

after change 150

You might also like