aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "local arrays" Watch "local arrays" New topic
Author

local arrays

meeta verma
Greenhorn

Joined: May 15, 2004
Posts: 19
Does default initialization apply local arrays ? I initially thought it doesn't but this example makes me accept it does..

int brr[]=new int[10];
public static void main(String []args)
{
ArrayDemo ad=new ArrayDemo();
int arr[]=new int[10];
System.out.println(arr[5]);
System.out.println(ad.brr[1]);
}
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
yes it does.

By default the array isn't initialised when you declare a local array variable BUT whenever you initialise an array variable (whether explicitly or in case of instance or class variables also implicitly the array ELEMENTS ARE implicitly initialised to their default values.

So had you written


the compiler would have complained about line 4 (possible use before...) because the array itself isn't initialised.
Comment out that line and everything works because the array ELEMENTS are initialised on you initialising the array as an int[4] on line 5.


42
Karel KoboojBot
Ranch Hand

Joined: Apr 09, 2004
Posts: 35


The code you have sent will compile and run because you do an explicit creation of an array. Anywhere you do a X id = new X[] then the array will be filled with the default value for type X. For numerics this means 0 in their respective format, for reference types this means the special value null. If you would have written the following int arr[]; instead of int arr[]=new int[10] then it will result in a compile time error saying "variable might not have been initialized".
meeta verma
Greenhorn

Joined: May 15, 2004
Posts: 19
So what is the difference in default initialization of local and member arays?
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
Correct me if I am wrong.
We can create arrays by doing
int[] arr= new int[2];
or
int[] arr={1,2,3};
There is no difference as far as initialization is concerned as long as we limit our thoughts to whether arr is a member or a local variable.

In both cases the arr is only a reference.
The actual array is created on the heap.
With local arr the reference arr is itself created on the stackframe.

If arr is assigned an array object the array elements may or may not have been explicitly initialised.
The arary elemets are not initialised in the case-int[] arr= new int[2];
If the array elements are not initialised they takeup default values.
These dfault values are 0 for primitive arrays and null for object ararys.

This behaviour of the array elemets is similar to that of object fields to some extent as far as the default values are concerened.
 
Consider Paul's rocket mass heater.
 
subject: local arrays