posted 17 years ago
Remember: Arrays are objects.
String[] str declares a variable called "str." Its type is String array.
When you say String[] str = null; str is assigned a null reference, so if you try to do anything with str, you will get a NullPointerException. You certainly can't reference a particular element in the array because no array exists.
But when you say String[] str = new String[5]; you are creating a new object. In particular, you are creating a new String array with a length of 5. (Note that array size is set when created, and cannot be changed.) A reference to this String array object is assigned to str, so now you can do something with it.
But there's another layer to this: An array is an object that holds references to other objects. When you create a new array using the above syntax, the array itself is created, but the references inside it do not point to anything. These references are null. But at least they exist, so you can assign them using the array reference -- for example, str[0] = "abc";
[ January 25, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org