Just like any other object reference, array references declared within a method must be assigned a value before use.
That statement is true. You can't do this:
Code:
int[] nums;
int[] temp = nums;
Since nums doesn't refer to anything(not even null), it doesn't make sense to assign nums to something else.
Just like any other object reference, array references declared within a method must be assigned a value before use.
That just means you must declare and construct the array.
That statement is false because you can do this:
Code:
int[] nums = null;
int[] temp = nums;
if(temp == null)
System.out.println(temp);
The code does not construct the array, yet the array reference nums is used in the code to do something.
Array elements are given their default values (0, false, null, '\u0000', etc.) regardless of whether the array is declared as an instance or local variable.
That statement is nonsensical. Array objects are not declared, they are created using new, e.g. new int[10]. If you create an array, and you don't initialize it yourself, then it will be created with default values--wherever its created in your code.
The array object itself, however, will not be initialized if it's declared locally
That statement is nonsensical. Array objects are not declared--you declare variables that will refer to array objects. Array objects are created using new, e.g. new int[10].
Is that statement trying to say that if an array object is created locally that it will not be initialized? That would be false, as a simple test can prove:
Code:
class Apple
{
public void test()
{
int[] nums = new int[10];
System.out.println(nums[0]); //outputs 0
}
}
public class AATest1
{
public static void main(String[] args)
{
Apple a = new Apple();
a.test();
}
}
In other words, you must explicitly initialize an array reference if it's declared and used within a method
That statement is false. You do not have to initialize an array reference. To "initialize" an array reference, you assign something to the array reference when you declare it, like this:
Code:
int[] nums = new int[10];
or
int[] nums = {3, 2, 4};
However, you do not have to "initialize" an array reference. You can do this:
Code:
int[] nums; //not initialized
...
...
int[] temp = {1, 2, 3};
nums = temp; //not initialized, rather "assigned a value"
but at the moment you construct an array object, all of its elements are assigned their default values.
The author already said that, and it's the only thing that he/she got right in the whole paragraph. The only thing worthwhile that you need to take away from that passage is that when you create an array with new, e.g. new int[10], the elements of the array are initialized with 0.
Another thing you might want to know that wasn't discussed is that a variable that is a data member of a class is automatically initialized with 0. For instance, if you have this class:
Code:
class Apple
{
public int num1; //data member of class
public void test()
{
int num2;
}
}
num1 will be initialized automatically with 0, but num2 will have no value and you must assign a value to it before you can use it in your program. As an exercise, change the type of num1 from int to int[], and then determine if the elements of the num1 array have been intialized with 0.
Oh yeah, and even though you don't have to, you should always initialize the variables that you declare. If you don't know what to set them to when you declare them, then set them equal to 0.
Bookmarks