Question:
Java passing by reference example?
just4mailing
2011-07-11 10:08:00 UTC
Hello,

(Please read this complete)

anybody please explain me with the help of simple example that what java passing by reference actally do ?

i know that , if a variable is passed, the method receives a copy of the variable's value.
The value of the original variable cannot be changed within the method. (pass by value)
but what does that statement mean ?

please explain with an example and don't copy anything from google, just provide me with your own simple and easy to understand example.,

thankyou
Six answers:
anonymous
2011-07-12 07:20:19 UTC
Say there is a variable at location 1200 with a value 123.



First we'll do call by value.

If we call a method and pass that variable as an argument, the compiler will find another location in memory, say 1700 and copy the contents/value of location 1200(123) to location 1700.

When the method starts executing it is given the location 1700 to work with. It can change the contents of location 1700 and never change the contents of 1200 where the variable's data started from. When the method returns, the contents of 1700 is destroyed to be used again by another method. The contents of 1200 is unchanged. It is still 123.



Now call by reference:

When the method is called it is given the location 1200 to work with. Now if the method changes the contents of location 1200, the original contents are changed. When the method returns the contents of 1200 will be as the method left it. The original value can be changed
Kung Fu Champion
2011-07-11 11:52:47 UTC
A reference is where the object is stored in the memory.

A value is where a value is stored in the memory.



passing by reference:

Object o = new Object(...);



You only make a copy of the variable to avoid privacy leak. When you create a copy of the variable, the copied variable will have a different reference. So, when you return the variable, the person who called the method has a copy of the variable and the person can change the variable without changing the original variable.



pass by value:

int i = 10;



Contact me if you need help.

Hope that helps.

=D
MagicIdiot
2011-07-11 17:57:39 UTC
Passing by value is like copying a variable's value, as you said.

Passing by reference is like passing a pointer to an object.



Say variable A is pointing to an object B

if you pass A through a method parameter. It copy's the variable's pointer. So now the variable in the method will also point at object B.
?
2016-05-14 19:23:41 UTC
You are pretty close by saying that reference is like pointer. To really understand what is going on, we have to look at memory. The variable in question is at some address in memory. When you pass by reference, you are basically getting that variable. When you pass by value, instead a new variable is effectively created in memory for the scope of your function.
anshu
2015-04-25 08:31:42 UTC
Java is always pass by value, even in case of objects-

When we say A a = new A(); a is actually a variable that holds the reference to the memory allocated to the created object. If that object a is passed as parameter to a method then in reality the copy of the value stored in a is passed. But that value happens to be a reference that is why if any change is made to the behavior of the object, that change is reflected globally.

public class A {

private int i;

//constructor

A(int i){

this.i = i;

}



void displayData(A obj){

// object manipulated

obj.setI(7);

System.out.println("Value of i inside method " + obj.getI());

}

public static void main(String args[]){

A a = new A(5);

System.out.println("Value of i inside main before method call " + a.getI());

a.displayData(a);

System.out.println("Value of i inside main after method call " + a.getI());

}

//getter

public int getI() {

return i;

}

//setter

public void setI(int i) {

this.i = i;

}

}



As exp. in this case output will be -



Value of i inside main before method call 5

Value of i inside method 7

Value of i inside main after method call 7



but at the same time state of the object can t be changed, so if we change the reference of the object in the method that won t change the reference in the calling method.
deonejuan
2011-07-11 12:13:48 UTC
Do.you.know.Objects.? This can only be explained from the viewpoint of a dynamic Object in RAM.



suppose a class of Car...

public class Car {

private int numEngines;

}

// in the class with the main()...

Car c1 = new Car(); // c1 is a reference BTW



if I send c1.numEngines to a method in main() to increase, or decrease, the number of engines

private void countEngines( int value ) {

mainCount += value;

}



That, without any disagreement from anyone is pass-by-value. Pass by Reference, on the other hand takes some thoughtful evaluation. When we slam around Objects and say Obj1.num = Obj2.num -- that can work, but to send this off into a method...



public void tricky(Point arg1, Point arg2)

{

arg1.x = 100;

arg1.y = 100;

Point temp = arg1;

arg1 = arg2;

arg2 = temp;

}

arg1 and arg2 will be a reference to external Objects. They are passed in "pass by value".

Point temp = arg1, is pass by reference

arg1 = arg2; again pass by reference



[from Java In a Nutshell]

"Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" As a result, you cannot write a standard swap method to swap Objects.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...