Prathima gaitonde wrote:Why I figured out this value, I have tried to explain in the attachment (as I was struggling to put it in words). Please help me solve this.
Roel De Nijs wrote:4a/ the setIt method is called with x=a (data members: [value=5, link=b]) and y=b (data members: [value=10, link=null])
4b/ lineA: y.link (which is null) is assigned to x.link (which was b). So only data members of object x (a) is referring to have changed: [value=5, link=null]
4c/ lineB: x (a) is returned
4d/ line8: the return value x (a) is assigned to b.link. So data members of object b is referring to: [value=10, link=a]
5/ line5: data members value of a.link and b.link are printed. But if we look at the data members of the object a is referring to ([value=5, link=null]), we see link is null; hence the NullPointerException being thrown
Prathima gaitonde wrote:My confusion lies in here, How come link of x(a) remains null? at line 4b/ lineA: a-->[value=5,link=b.link], that means it is referring to b.link object isn't it? Which is at this point null I agree.
but, when at line 4d/line 8: the return value changes b.link's value which is 'a', a.link will be pointing to object 'a' is it not right? Sorry for not getting your's and Sergej Smoljanov's detailed explanation.
Prathima gaitonde wrote:My confusion lies in here, How come link of x(a) remains null? at line 4b/ lineA: a-->[value=5,link=b.link], that means it is referring to b.link object isn't it? Which is at this point null I agree.
but, when at line 4d/line 8: the return value changes b.link's value which is 'a', a.link will be pointing to object 'a' is it not right? Sorry for not getting your's and Sergej Smoljanov's detailed explanation.
Prathima gaitonde wrote:1/ Reference variable a refers to a Holder object with data members value = 5 and link =b.link=null(At this point a.link--->null<---b.link)
2/ Reference variable b refers to a Holder object with data members value =5 and link =a which intern(b.link.v=5), [ wait a minute, what about b.link.link(which is a.link)?!!! is this null?]
Prathima gaitonde wrote:
My confusion lies in here, How come link of x(a) remains null? at line 4b/ lineA: a-->[value=5,link=b.link], that means it is referring to b.link object isn't it? Which is at this point null I agree.
but, when at line 4d/line 8: the return value changes b.link's value which is 'a', a.link will be pointing to object 'a' is it not right? Sorry for not getting your's and Sergej Smoljanov's detailed explanation.
Prathima gaitonde wrote:One last thing, can you please teach me, How to write the diagram? that you draw, as your first reply to this post. As I struggle to do that and finally draw it on paper and scanned it.
Prathima gaitonde wrote:As Sergej Smoljanov, has drawn the picture very neatly I presume he has done using it some kind of a software, I tried it in power point, dint work, hence wanted to know and learn the technique of drawing it neatly.
Roel De Nijs wrote:
Now let's start with evaluating the main method:
1/ line5: a new Holder instance is created and is referred by a. Data members: [value=5, link=null] 2/ line6: another new Holder instance is created and is referred by b. Data members: [value=10, link=null] 3/ line7: b is assigned to a.link. So data members of object a is referring to: [value=5, link=b] 4/ line8: setIt(a, b) is assigned to b.link. So the next step is to evaluate the setIt call to know the effect on the data members... 4a/ the setIt method is called with x=a (data members: [value=5, link=b]) and y=b (data members: [value=10, link=null]) 4b/ lineA: y.link (which is null) is assigned to x.link (which was b). So only data members of object x (a) is referring to have changed: [value=5, link=null] 4c/ lineB: x (a) is returned 4d/ line8: the return value x (a) is assigned to b.link. So data members of object b is referring to: [value=10, link=a] 5/ line5: data members value of a.link and b.link are printed. But if we look at the data members of the object a is referring to ([value=5, link=null]), we see link is null; hence the NullPointerException being thrown
Mushfiq Mammadov wrote:I read this post carefully, line5, line6, line7, line8 are consistent line1, line2, line3, line4 respectively. Maybe you intended to write as this, Roel.
Mushfiq Mammadov wrote:In line1 y is assigned to x but the value of str (which a refers) doesn't change because of pass-by-value. I followed this rule. But I saw that if we put comment line1 and uncomment line2 the output would be b b ...
Don't get me started about those stupid light bulbs. |