aspose file tools
The moose likes Beginning Java and the fly likes Uninitialized array access : one vs multi dimension Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Uninitialized array access : one vs multi dimension" Watch "Uninitialized array access : one vs multi dimension" New topic
Author

Uninitialized array access : one vs multi dimension

Ek Chotechawanwong
Greenhorn

Joined: Jun 23, 2002
Posts: 7
Hi,
I have a basic language question about array access. There seems to be difference in how java compiler handle uninitilized member between one and multi dimensional array. (I use Sun - j2sdk 1.4.2_03 on Win2k).
In the one dimension array,

In this case I cannot compile the program.

ArrayTest.java:4: variable a might not have been initialized
System.out.println("a[0] = " + a[0]);
^
1 error

However, in case of 2 dimensional array, I can compile but I got runtime exception (NullpointerException).

In this case I can run the program but got NullPointerException.

Exception in thread "main" java.lang.NullPointerException
at ArrayTest.main(ArrayTest.java:4)

Question
Could you please explain why the behaviour is different?
Thanks so much in advance.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
ekc,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26184
    
  66

In the second one, you did initialize the array (a).

It is a valid case to have an array that contains all null elements. For example, you may want to populate it later. So the compiler doesn't know for sure whether the array has been initialized.
In the first example, the array was not initialized at all. This is a case that the compiler can identify.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ek Chotechawanwong
Greenhorn

Joined: Jun 23, 2002
Posts: 7
Hi Jeanne,
I think just allocates the first dimension aka. array of 2 int[] elements however it doesn't explicitly construct the second dimension of each - it doesn't say how many int elements will be in each int[] array.
Therefore, it's the same as the first case to me.
If it's a kind of java compiler limitation/design issue, I would be OK with the difference. But I am afraid, I am missing something on language concept.
In other words, I see that javac can check whether one dimensional array is initilized or not. Is there any reason why java compiler doesn't check the subsequent dimension of the multi-dimensional array?
Thanks,
Ek
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Uninitialized array access : one vs multi dimension
 
Similar Threads
Array
Question on Multidimensional Array
Arrays
Variable initial values
Multi dimension array confusion