Hello team!
In this lesson we will discuss with you a very important topic which even some more experienced software
engineers can't understand.
But I will explain you this topic with simple examples and you will remember it easily.
Today we will talk with you how data is passed to the method in Java by value or by reference.
Why?
This question is important because in different programming languages, you can decide whether you want
to pass value to the method by value or by reference.
This might affect the state of the data which was supposed to be modified after specific method call.
So you already understand that we can pass arguments by value and by reference.
What does pass by value mean?
Passing by value means that the value of the method parameter is copied into another location of your
memory, and when accessing or modifying the variable within your function, only the copy is accessed
or modified and the original value is left untouched.
Imagine empty cup and specific function which you want to use to fill this cup with coffee.
In case we pass by value, we copy this cup inside the function and all changes with this cup affects
only cup which was copied.
Then what does pass by reference mean?
Passing by reference means that the memory address of the variable, or it is better to say a pointer
to the memory location is passed to the method.
In case we pass by reference, we pass the address where this cup is located.
And when I fill the cup inside the function, I also fill the original cup.
And now please remember the rule in Java.
Primitive and reference types of data are passed by value.
That is the only right answer on the interview.
You can read in internet that Java objects are passed by reference, and primitive types are passed
by values.
That is not true on the simple examples.
I will prove you this.
Here I have int value which is equal to ten.
I have method which takes int as a parameter inside it.
We want to increase I by 100.
What value of I will be in console here?
After the method call, the value will be ten.
Why?
Because in Java everything is passed by value.
Here when I called method, the copy of I variable was created and when I increased I by 100 value,
the result was assigned to the local I variable, but not to this one.
These two are different.
They remember in the lesson when we discussed Java memory model, I told you about stack memory.
When this method is called copy of the I was created in stack.
So in this case how I can change the value of I.
Here the only way to do this in Java is to use return statement.
Your message should return some value which will be assigned to the variable.
Like in this example I call method change int, I add 100 and I return the value from this method which
is assigned to I variable.
Here.
Let's move on.
Now I have array of ints and you remember that arrays are reference type of data.
I have method which has some logic inside to change state of the array.
I want to make second element in this array equal to 200, but not two as in original array.
Let me print this array to console after this method call.
What do you think will be the second element?
2 or 200.
And it is 200.
You might be wondering how this could happen if in Java we pass everything by value, then modification
which we did in the method should not affect original array.
Let's understand what has happened here.
I call the method and pass reference to the array object.
Attention.
I pass the reference to the array, but not the array, and copy of the reference is created here.
I can get access to the array through the reference.
So here I use the reference to get access to the same array as here.
These two variables reference to the same array object in the heap memory.
Probably now you want to get proofs that this is a copy of reference, but not copy of the array.
I have another method with name clear array.
It takes an array as a parameter and makes this variable reference to null.
Let me call this method with our array.
Now I print it to console.
What will be printed to console null or values from array?
Values from array is on place.
Why?
Because this is copy of reference and now local method variable which exists only in scope of this method.
References to null.
No problems at all.
This reference still refers to the object on heap memory.
And now you can be sure that even when we pass objects in Java to the method, we still pass by value
because we pass reference to the object and we pass value of the reference.
Hope examples from this lesson help you understand the topic better.
Let's recap what we have learned today.
Today we learned what does pass by value and pass by reference mean.
We also understood that in Java everything is passed by value.
On the examples, we saw how primitive and reference types passed by value.
That's all for today.
Thanks a lot for your attention.
See you in the next lesson.
Your.