Add- on Question: "Is Java “pass-by-reference” or “pass-by-value”?"

Java - Top 10 Most Viewed Questions on Stack Overflow Add- on Question: "Is Java “pass-by-reference” or “pass-by-value”?"
15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

The last question in this course, is an add on question. And I've edited because in the past, I've read it very often the wrong way or people claimed the wrong things about the fact if Java is to pass by reference or pass by value language. And so this question is always also on StackOverflow. But I don't know the exact position regarding the most viewed question. But anyhow, I want you to, to know what is right. And I want you to, to be able to response this question the right way, in the future.

So let's have a look at the both terms pass by reference and pass by value. Me personally, no, there's two techniques with the term called by reference and call by value, but it means the same pass by reference and call by reference. It's the same. So, let me tell you first that Java is always an only call by value. So, if you are only familiar with Java, you don't know call by the call by reference technique. So, yeah, Java is only called by value.

This is very important, what is called by reference or what does both techniques mean? Both techniques appear when you are invoking a method and you want to pass a parameter to this method. So you have created an object beyond the method and have a reference to this object and then you Pass the reference to the method and the method do anything with it with the object or the reference the object where the reference is pointing to Yes. And caught by reference means that you are passing an expert says reference to the to the reference to the method. So, you have a pointer that points not to the object itself, but to the reference of the the object Yes. And color by value means that you if you are invoking the method with a parameter and the parameter of the method is the copy of the reference of the object beyond the method.

So, let's have a look at both techniques. We start with call by value because Java Java is callable value. You can in Java you can pass either primitive types or real raw object types to the method and the easy way to demonstrate it is for primitive types. So let's start with primitives. We are creating an variable an int variable with the value zero. And then we are calling or invoking a method and pass by pass in variable in it.

And in this what what does this do, because Java is called by value, it is copying the value of i the variable i to the variable Param I so if we Are invoking the increment method Param I and I do the have the same value now. And then within the method Param is increment incremented by one. So now Param is one, but because we copied the value and variable one I sorry, isn't affected by this incrementation. So, after the method is run through, we are system out print line variable i and it is still zero. Yeah, because Java is called a value and param i is just a copy of I am not a reference of i. So, this is how Java works with primitive types.

I think it's easy to understand A bit more confusing is with with if you are working with objects So, what is Java doing here in line nine we are creating an object and the reference or one on this object then on our workings working storage on the heap it is creating an object one and a reference or one that points to these to this object one and now we system out print liners and okay it says alright this object with the hash code Alright. So now we are invoking method with with passing a one as parameter. And because Java is called by value, it is copying the value of the reference or one and cause This copy Param So, if you are taking a look at our Java works, you see object Param the reference object Param is now a copy of the object or one reference.

So, what does this mean? object Param an object or one is pointing to the same object on the heap or the working storage is. So, a one and param is both are both pointing to the same object. So, if we are now reassigned, reassign the parent with a new object, so we are creating a new object in line two, we are calling the reference or to what has happened. We are and then we are system of print line the object or to write it has different hash code and now we are reassigning The old tool to the Param reference and if we are taking a look inside of Java what happens now object Param the reference is pointing to object to to our newly created object and object one is still pointing on object one in the in the heap. So, if we are leaving the method and we are system out print line or one it is still the same, it is still has still the same the hash code the same hash code.

Okay. So this is how Java works call by value. If Java would be caught by reference, it would look different than by call by value for for sure. So, let's take a look how it would look like. Java would be called by reference, but let me tell you, the code works different. So this example is only for your imagination.

And there are languages like c++, for example, where the programmer where you can decide if you want to invoke a method with call by reference, or if you want to invoke a method with call by value. So you have both opportunities. Yeah, but in Java, you don't have but yeah, this example should show you how Java would look like if it would be called by reference. So, the example looks pretty much the same. Like the example earlier, with call by value. So we are creating an object and an the reference to the object or one that affects that There is an object in the working storage and the reference a one is pointing to the object one all right.

So, we system or print line the object one and see okay it system out print lines object one all right. Now we are calling the method and passing a one as parameter to it. And if Java would be called by reference, then the parameter and called Param would be a pointer on the reference oh one. So, it would look like this Param it within the method is pointing on the reference outside method or one and a one is pointing on object one. So, now, we are creating an a new object with a new reference or two And system out print line this one and it says okay I am object two and then we are reassigning the power arm with o two. So, this would affect if Param would pointing to a one that Param and a one is now pointing an object to.

So, yeah with call by reference you are able to edit or to change references beyond the method within the method, yes, so, a one is a reference outside method and because you are able to, to pass a one as parameter with with the technique call by reference, you are able within the method to change the reference outside and beyond the method that will be passed by reference or call by reference. Yes, but this is only for your imagination. Java works like this. And you aren't able in Java to to change the reference with in method that affects or would affect reference outside of method. All right. But what you can do in Java is that you you can edit the object within a method.

So here in this example, we are trying to reassign or to change the reference with a new object. This isn't possible, but because when we are calling the method, it looks like this object a one and object Param is pointing to the same object. And now in the method we are able to edit And to change things in the object one, yes. And that's Yeah, for this case, I have made an example. So what do we do here, we have a class called foo, and a private string string, str. Ghana.

Yes. And here we have a constructor. And we pass a string in here and have a setup method where we can set the internal string with a new value. And we have a two string method that returns only the string, internal string. So now we have two different methods. In the first method, we try to reassign the variable foo with a new object foo.

And in the second second method, we are Only changing the object. So we don't we don't touch the reference, we only change the object by setting a new string. And so we know Java has Koba value. So how does Java work? Alright, we are creating a full one reference to a new foo object with the internal string foo one. And we are calling the method foo one.

So we try to reassign the reference foo and No wonder it doesn't work. So after the invocation of the method for one as is pointing to to the object for one and yeah to to this object with the with the content for one and this is why for one, this print, printed alright Now we want to change the object, we are calling the method two. And we are here. And now the reference foo one, and the reference foo is pointing to the are pointing to the same object. So it looks like foo. One is pointing to this, and foo is pointing to that is, and this is why we are able to change something on the object.

And so we now set a new string. So we are calling the method set string. And we pass as text new string for foo. And this affects that this object called foo, one, or reference by foo, one is changed with the string new string for foo and if we now system out print lines, One we see all right new string Fufu. So yeah, this is showing you how call by value works. And as I said 100 times for Java works this way Java works only call by value.

All right, this is it for the question.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.