Page 1 of 2
About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Aug 01, 2012 9:08 pm
by dtchky
This question is categorized as easy, though having read many Java books and programmed in a lot of languages for a lot of years, it might be that I'm simply reading the wrong books

though this question seems more like a fairly dusty corner of the language rather than easy. Wondering if you could expand a little on
a.link = b; I had no idea you could do this and don't quite understand how it works. I can see what the result is by picking a.link apart, but what mechanism makes the assignment work?
Code: Select all
Holder link;
public Holder(int val){ this.value = val; }
public static void main(String[] args){
final Holder a = new Holder(5);
Holder b = new Holder(10);
a.link = b;
Thanks.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Aug 01, 2012 10:11 pm
by admin
link is a field in class Holder. a is a variable of class Holder. So a.link refers to the member field link of the Holder object pointed to by a.
b is another variable of class Holder. It also points to an object of class Holder.
Therefore, in a.link = b; you are assigning the object pointed to by b to a.link.
I hope that explains it.
Regarding toughness, you are right. It doesn't look like an easy question
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Aug 01, 2012 10:44 pm
by dtchky
Definitely clears things up now. Conceptually I had been thinking of it as chaining variables of class types together. Quite a difficult one to google. : )
Thanks greatly, appreciate the explanation.
Brendan.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Aug 01, 2012 11:02 pm
by admin
The Holder class is actually a very basic (and well known) building block of a linked list. It is usually named "Node" and not Holder. Please google linked list to learn more.
Though not important for this exam, understanding how linked lists work is something that a programmer must know. You mentioned that you've been programming for years and I am surprised to know that you didn't recognize it.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Aug 01, 2012 11:42 pm
by dtchky
I would humbly disagree with you good sir, purely because I think it much wiser to avoid presumption that any construct translates cleanly from one programming language to another. Clean slate. Starting fresh. I'm familiar with linked lists and how they work in other languages, programming 101, sort algorithms, fodder for bog standard interview questions, however at this point in time, this particular question doesn't jump out at me as being related to linked lists other than the word 'list', which is just a variable. As you say, "not important for this exam", thus quite a critical statement when OCAJP is the end goal. Intricate knowledge of the collections framework is a couple of items away on my check list.
Thanks again, appreciate the reply.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Aug 01, 2012 11:50 pm
by admin
You are right, it is not important for this exam and is not really relevant for this question either. There is no presumption of linked lists required for answering it. I was just surprised. That's all. The question will be improved to avoid such confusion.
thank you for your valuable fedback!
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Jan 16, 2013 3:18 pm
by lordnovas
admin wrote:link is a field in class Holder. a is a variable of class Holder. So a.link refers to the member field link of the Holder object pointed to by a.
b is another variable of class Holder. It also points to an object of class Holder.
Therefore, in a.link = b; you are assigning the object pointed to by b to a.link.
I hope that explains it.
Regarding toughness, you are right. It doesn't look like an easy question
HTH,
Paul.
Hey Paul, I was a bit confused with this question as well. What I don't get is how 'a' became null in the first place. When I code the question and check to see if 'a' is null with print statements, it appears that 'a' becomes null when it references .link (a.link). Is this because the variable "link" is declared as a reference to the Holder object, but is not assigned to a Holder Object? So like is the value of "link" null, which is why 'a' is now null? or is 'b' null and when "a.link" is assigned the value of 'b' it in turn becomes null?
Sorry if my question is confusing, please help if you can.
cheers,
j-
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Thu Jan 17, 2013 11:31 am
by admin
'a' is not null. Where does it say that a becomes null?
a.link is null and that is why a.link.value throws NullPointerException.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Mar 06, 2013 4:27 am
by The_Nick
Amazing question, it's also worth to play a bit with this class to understand how an member object variable works within another object. Also the final keyword very well explained.
However I agree with who above stated that the degree of difficulty does not reflect the reality. I would say at least tough.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Mar 27, 2013 12:03 pm
by baxhuli
That's how I understood it.
We assign a value to a.link (a.link=b) whereas b.link is null because we do not assign nothing to it.
When the program tries to do "b.link.value" as b.link has not been initialized we get a error at runtime.
The method "setInt" does not even get executed. I think there is a mistake in the explanation. However this question should at least be tough.
Thank you.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Thu Sep 19, 2013 10:24 am
by TheSarjo
Problem arise when it's invoked the setIt method.
a.link points to the same object b points.
b.link points to the object returned by method setIt.
setIt takes two objects, (referenced by a and b) BUT sets a.link (x.link) to b.link(y.link). But b.link points to nothing!
So, a.link now also points to nothing. So nullpointerexception.
nice question!
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Jan 15, 2014 2:05 pm
by thupten
@baxhurli The method setIt(a,b) does get executed.
Code: Select all
public static Holder setIt(final Holder x, final Holder y){
x.link = y.link; //makes a.link to null so now a is an object with value=5, link = null
return x; //returns a. This is important because it is being assigned to b. So b.link.value gives a.value, i.e. 5 because of this return statement.
}
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Fri May 02, 2014 1:21 am
by __Bill
The java content of this question is simply "What happens if you use a null pointer." Null "reference" I mean.
It's sooo frustrating to miss such a simple question because it's buried under a half-mile of knots. The worst is working through nested loops with nonsense breaks and continues with pencil and paper to figure out what it will print.
I suppose they're trying to test something more than java knowledge, and I know this example is typical of the real tests, so it speaks well of the example. I feel almost like it's a twisted fraternity initiation.... Oh well, it's all in fun I guess. - Bill
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Tue Jun 17, 2014 1:46 pm
by vchhang
Code: Select all
class Holder{
int value = 1;
Holder link;
public Holder(int val){ this.value = val; }
public static void main(String[] args){
final Holder a = new Holder(5);
Holder b = new Holder(10);
a.link = b;
b.link = setIt(a, b); //(1)
System.out.println(a.link.value+" "+b.link.value);
}
public static Holder setIt(final Holder x, final Holder y){
x.link = y.link; //(2)
return x; //(3)
}
}
Would you explain why b.link is pointing to null?
1. At //(1) setIt() is called with 'a' and 'b'.
2. At //(2) x.link which is pointing the same location as a.link. a.link points to the Object reference by 'b'. The object reference by 'x' which is the same object referenced by 'a'.
Therefore,
3. At //(1) b.link is point to the object reference by 'a'.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Tue Jun 17, 2014 8:38 pm
by admin
When you create b = new Holder(); b.link is null. It is not being assigned a value anywhere. So it is null.
I have no idea what you are saying at 2. At //2, when you execute x.link = y.link; you assign y.link to x.link. y.link is null (because b.link is null as explained above), so x.link becomes null. x is nothing but a reference to the same object referred to by a. so a.link becomes null. Hence, NPE is thrown when you do a.link.value.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Jun 18, 2014 8:24 am
by vchhang
at //3 we are returning x which is the object referenced by a. When the method setIt returns, b.link is pointing a. Where am I going wrong?
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Jun 18, 2014 8:41 am
by admin
b.link is null before the invocation of setIt. b.link is null even when you are inside the setIt method. That is why null is assigned to a.link.
b.link becomes non-null (pointing to a) AFTER setIt returns i.e. AFTER //3 (not at //3).
You might want to run it in a debugger and see the values step by step.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Mon Apr 20, 2015 9:37 am
by Sergiy Romankov
Tell me please if I understand it right
When creates b = new Holder(), field b.link = null
than fetching to method setIt() reference b and y are both reference to
same object, which field link is null
By assigning y.link to x.link implicitly a.link became null
and when the program tries to get a field a.link.value of a.link we get NullPointerException
Thanks,
Sergey
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Fri Feb 19, 2016 8:55 am
by NickWoodward
I got the question right, so think I understand the main points, but....
I can't see why trying to access a.link.value would through an NPE. Yes it's null, but
String s = null; System.out.println(s); doesn't throw that exception.
I thought it might be applying the + operator, but it also occurs with System.out.println(a.link.value);
does that make sense?
thanks,
Nick
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Fri Feb 19, 2016 9:08 am
by admin
NickWoodward wrote:I got the question right, so think I understand the main points, but....
I can't see why trying to access a.link.value would through an NPE. Yes it's null, but
String s = null; System.out.println(s); doesn't throw that exception.
I thought it might be applying the + operator, but it also occurs with System.out.println(a.link.value);
does that make sense?
thanks,
Nick
You need to understand that accessing a null reference always causes a NullPointerException to be generated. Always. No exceptions.
Basically, "access" means, the use of the dot operator. someReferenceDOTsomeMember. If you try a.link.value and if a is null or a.link is null, it will generate a NPE because you are trying to access a null.
Now, regarding why System.out.println(s); does not throw NPE, you have to ask yourself, are you accessing null here? The fact that it does not throw a NPE itself tells you that it is not being accessed. Then how does it print "null"? The only way that can happen is if the code inside the print method checks whether the reference is null before trying to access it. Something like this: if(ref == null) print("null") else print(ref.toString());
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Fri Feb 19, 2016 9:30 am
by NickWoodward
ah of course, i'm confusing access with passing the variable as an argument.
thanks, great answer!
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Dec 07, 2016 5:04 am
by roosdebie
Holder link;
This 'link' is declared, but ininitialized. So (if I am correct): If an Object reference has been declared but not instantiated, its value is null.
So this is why b.link; is 'null' (and a.link if it was not assigned to the Object reference 'b').
Took me a while...
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Mon Feb 27, 2017 7:13 am
by bniky1
I have read the previous comments and still don't understand a.link = b. Does this mean a.link = b.link?
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Mon Feb 27, 2017 9:23 pm
by admin
bniky1 wrote:I have read the previous comments and still don't understand a.link = b. Does this mean a.link = b.link?
No, a.link = b does not mean a.link = b.link. It means a.link points to the same object to which b is pointing.
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Posted: Wed Dec 26, 2018 10:51 pm
by crazymind
"x.link = y.link, x.link becomes null because y.link is null so a.link.value throws NullPointerException."
Does it mean you do: null = null; will throw a NPE ?