Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initializing multidimensional array

 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the following code where I have created a multidimensional array of references in which I have not instantiated(commented in the code) the second dimension of array:



here compiler doesn't have any problem but there is runtime exception. My question is that why compiler doesn't complain that we can't use aor[0][0] before instantiating because after instantiating only reference starts pointing null(i.e. at line 1).
[ August 30, 2006: Message edited by: Vaibhav Chauhan ]
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this code generates compilation error that local variable is not initialised. How the above case is different from this one.
[ August 30, 2006: Message edited by: Vaibhav Chauhan ]
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The elements of the array will be initialized to its default values 0 for primitives , null for Objects ..

Here all the string references are initialized to its default values.Like this
in

aor[0],aor[1]... aor[4] will be initialized to null, when you access anything using null reference NullPointerException will be thrown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic