Copy by value in java

JavaMadeSoEasy.com (JMSE)

In this core java tutorial we will learn in lots of detail that java is purely Pass by value and not Pass by reference with example, programs and diagrams. We will learn w hat is Call by value (Pass by value)? And how java is call by value? What is Call by reference (Pass by reference) ? And what happens in Call by reference? And how java is NOT call by reference? Programs to demonstrate Call by value (Pass by value) in java. And, Program to demonstrate Call by value (Pass by value) — reference to object is passed by copy in java.

Question : What is Call by reference (Pass by reference) ? And what happens in Call by reference? And how java is NOT call by reference?

Program 2 — to demonstrate Call by value (Pass by value) — reference to object is passed by copy in java

2.1) Let’s understand by diagram how in above program (program 2) reference to object is passed by copy in java >

Program 3 — In continuation to program 2 — to demonstrate Call by value (Pass by value) — reference to object is passed by copy in java

3.1) Let’s understand by diagram how in above program (program 3) reference to object is passed by copy in java >

First I will like to discuss what is difference between call by value and call by reference. And what happens in call by value and reference in java. Does object changes or not in java?

Answer : Call by value means Copy of an argument is passed to method . Therefore, any changes made to the argument in passed method won’t change the value of original argument.

Читайте также:  Persistence in java beans

In java when any changes are made to the argument in passed method the value of original argument is not changed. Hence, Java is call by value.

Question : What is Call by reference (Pass by reference) ? And what happens in Call by reference? And how java is NOT call by reference?

Answer : In Call by reference, reference of argument is passed to method . Therefore, any changes made to the argument in passed method will change the value of original argument as well.

In java when any changes are made to the argument in passed method the value of original argument is not changed. Hence, Java is NOT call by reference.

When the method or constructor is invoked ( §15.12 ), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor. The Identifier that appears in the DeclaratorId may be used as a simple name in the body of the method or constructor to refer to the formal parameter in java.

In this example first we assigned some value to int i and String str, then passed them to method m(),

Then we printed i and str in main method but changes were not reflected in main method. So, that shows java passed value by copy in java .

System. out .println( «i background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ i + «, str background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ str );

System. out .println( «i background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ i + «, str background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ str );

System. out .println( «i background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ i + «, str background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ str );

Now, let’s analyze about call by value example — In above example Copy of an argument is passed to method m() and therefore any changes made to the argument in passed method i.e. m() didn’t change the value of original argument. in java

Program 2 — to demonstrate Call by value (Pass by value) — reference to object is passed by copy in java

Before reading below text I will like you to be careful about the terms in which reference is used. Do not mess up between reference to object and call by reference in java .

Reference to object is passed by value (i.e. copy of reference is created and passed ), reference to object is not at all passed by reference in java .

Here in the below program, a is reference to object Emp a is passed by value (i.e. copy of a is created and passed ), a is not at all passed by reference , it may look like that a is passed by reference but actually that doesn’t happens at all copy of a is created and that is passed to method m() in java.

System. out .println( «a.id background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ a . id );

System. out .println( «a.id background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»>+ a . id );

System. out .println( «b.id background-color: transparent; color: black; font-family: «consolas»; font-size: 13.333333333333332px; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;»> + b . id );

2.1) Let’s understand by diagram how in above program (program 2) reference to object is passed by copy in java >

When method m is called, a is also passed to method m, but a is not directly passed to method m(), first copy of a is created and then passed to method m.

b.id is assigned a new value, a and b are referring to same object so any changes made to b will be reflected in a as well.

We cannot change the reference in the called method i.e. the method in which reference to the object has been passed by copy, If in case reference is changed in called method, the copy of reference in the called method will start pointing to the new object but original reference will keep on pointing to old object only in java. We will understand this in next program.

Program 3 — In continuation to program 2 — to demonstrate Call by value (Pass by value) — reference to object is passed by copy in java

Reference to object is passed by value (i.e. copy of reference is created and passed ), reference to object is not at all passed by reference.

Here in the below program, a is reference to object Emp, a is passed by value (i.e. copy of a is created and passed ), a is not at all passed by reference , it may look like that a is passed by reference but actually that doesn’t happens at all copy of a is created and that is passed to method m() in java.

We cannot change the reference b in the method m(), if in case, b is changed, b will start pointing to the new object but original reference i.e. a will keep on pointing to old object only in java.

Источник

Оцените статью