JavaRush /Java Blog /Random EN /Passing Parameters in Java
vincent_vega
Level 24
Gainesville

Passing Parameters in Java

Published in the Random EN group
A variable in Java is a container with a value in it. So what does it mean to "pass" a variable? And what is the difference between primitive and reference data types.
Passing parameters in Java - 1
We'll get to that later. Let's start with a simple assignment first. What this code does:
int х = 3;
int у = х;
On line 1, a variable x of type int is created and assigned the value 3. On line 2, a variable y of type int is created and assigned the value of variable x . In the future, the variable x does not affect y in any way . Java copies the value of x (3) and places that copy into y . This is passing a parameter by value. You don't write one variable to another. The value is copied and assigned to a new variable. Expression y = x; Does NOT mean "write x to y ". It means "copy the value inside x and write that copy to y ." If later I change y :
у = 34;
Will this affect x ? Of course not. x still has the value 3. If I change x later :
х = 90;
How will this affect y ? No way. They are not related in any way after the assignment has been made (COPYING the value). What about reference types? How do they work? Not that difficult, in fact the rule is the same. Links do the same thing - you get a copy of the link. So if I say:
Cat A = new Cat ();
Cat B = A;
Link A is copied to link B. This does not apply to the object - you still only have one object. But now you have two different references controlling the same Cat object . Now let's look at passing parameters to methods. Java passes parameters by value. Always. This means "copy the value and pass the copy." For primitive types it's easy:
int х = 5;
doStuff (х); / / Передать копию х (meaning 5) в метод doStuff
The doStuff method looks like this:
void doStuff (int у) {

   / / Действия с 'y'
}
A copy of the value of x , i.e. 5, is passed to the doStuff() method . The doStuff() method has its own variable called y . The y variable is a new, different variable. With a copy of what was in x at the time of passing it to the method. From now on, y and x have no effect on each other. When you change y , you don't affect x .
void doStuff (int у) {

   у = 27; / / Это не влияет на 'х'
}
And vice versa - if you change x , you will not change y . The only thing x did in this case was copy its value and pass it to the doStuff() method . How does "pass by value" work with references? Too many people say, "Java passes primitive types by value and objects by reference." It's not what they say. Java passes everything by value. With primitives, you get a copy of the content. With links you also get a copy of the content. But what is the content of the link? Remote control. Facilities for managing/accessing the facility. When you pass a reference to an object to a method, you are passing a copy of the reference. Remote control clone. The object is still sitting in the pile where it was created, waiting for someone to use the remote control. The facility does not care how many remotes are "programmed" to control it. The only people who care about this are the garbage collector and you, the programmer. So when you say:
Cat A = new Cat ();
doStuff (А);

void doStuff (Cat B) {

   / / Использование B
}
There is only one Cat object . But now two remote controls (links) can access the same Cat object . So now whatever B does to the Cat object will affect the Cat pointed to by A , but it won't affect the contents of A ! You can change Cat using the new link B (copied directly from A ), but you cannot change A. What the hell does this mean? You can change the object that A refers to , but you cannot change A's reference - redirect it to another object or null . So if you change the reference of B (not the actual Cat object that B references , but the value of the reference itself) you will not change the value of A . And vice versa. So:
Cat A = new Cat ();
doStuff (А);

void doStuff (Cat B) {

   B = new Cat (); / / Не повлияет на ссылку A
}
It simply means that B points to another object. A is still happy. So repeat after me: Java passes everything by value. (Okay, one more time... with feeling.) Java passes everything by value. For primitive types you pass a copy of the current value, for object references you pass a copy of the reference (remote control). You never pass the object. All objects are stored on the heap. Always. Now brew a hefty cup of coffee and write some code! Original article.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION