This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why the program hangs up at runtime

 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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' .

Here's the code:



Kindly anyone explain this to me..
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul and Matthew
Is it that the program would have run fine if instead of Integer we had int as instance variable?
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it that the program would have run fine if instead of Integer we had int as instance variable?


yes! it runs fine if you use int because instance variables of type "int" are given 0 as their default value.
 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What might cause this program to hang?

Is it that Case 0: might never get invoked?

Is there any other reason?
 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glen Iris wrote:What might cause this program to hang?

Is it that Case 0: might never get invoked?

Is there any other reason ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic