This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initializing multidimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initializing multidimensional array" Watch "initializing multidimensional array" New topic

initializing multidimensional array

Vaibhav Chauhan
Ranch Hand

Joined: Aug 16, 2006
Posts: 115
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

Joined: Aug 16, 2006
Posts: 115

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

Joined: Dec 06, 2004
Posts: 116
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

aor[0],aor[1]... aor[4] will be initialized to null, when you access anything using null reference NullPointerException will be thrown.

Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: initializing multidimensional array
Similar Threads
multidimensional array
Multidimensional ArrayList
Multidimensional array
Multidimensional arrays
Constructing multidimensional arrays