• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

local arrays

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]);
}
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what is the difference in default initialization of local and member arays?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic