The following code in from exercise of K&B . The answer shows that it might throw Nullpointer exception as well as program might hang without ever completing.
I don't understand why it will throw Null Pointer Exception. All though answer explains since Integer i is not initialized it.. But according to my knowledge instance variables are assigned value '0' .
Divyya Joshi wrote:But according to my knowledge instance variables are assigned value '0' .
This must be today's catch-phrase: "According To My Knowledge". I seem to be spending my whole morning pointing out that people's "knowledge" is incorrect.
And yes, it's incorrect. Instance variables are only assigned value '0' if they are of type char and the programmer explicitly assigns that value.
Paul might be slightly pickily distinguishing between '0' and 0 (), but he's right that this is not what happens. Reference member variables get initialised to null if a value isn't specified, and Integer is a reference variable.