Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

array initialization

>>>Before initialization arrays are always set to contain default values wherever they are created.


>>>Learning this part of the objective requires understanding a simple rule. The value of the elements of an array of any base type will always be initialised to a default value, wherever the array is defined. It does not matter if the array is defined at class or method level, the values will always be set to default values. You may get questions that ask you what value is contained in a particular element of an array. Unless it is an array of objects the answer will not be null (or if they are being particularly tricky NULL).




I was reading above lines from link
http://www.jchq.net/certkey/0405certkey.htm

did not understand it clearly.

Does it mean array no matter defined at class or method level assigned default value of Boolean or String etc object or numeric type of the array      .

like this program printed false
package com.vaannila.student;
public class MyVal{
public static void main(String argv[]){
        MyVal m = new MyVal();
        m.amethod();
}


public void amethod(){
boolean b[] = new boolean[5];
System.out.println(b[4]);
        }

}
 Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of gudii9

ASKER

>>>>In case of arrays this rule does not apply.
even though

boolean b[] = new boolean[5];

is not instance array, but rather inside method,
neveretheless its elements
are initialized to default value false


why is so weird with arrays. please advise

I think this is because array in Java is treated like an object;
and its values are initialized to the deafult values more or less in the same way
as instance valriables of the object are initialzes as default values.

Once you create an Objeect you need to have all its instance variable initializaed.
Now consider array elements as instance variables  of array object and
then they should be initialized (to the default values) at the array creation time.
Avatar of gudii9

ASKER

so basically no matter where you create the array the elements of the array will always be initialized to the default value of the element type right.

>>>Once you create an Objeect you need to have all its instance variable initializaed.Now consider array elements as instance variables  of array object

can you please elaborate on this
Yes, you understood it correctly - the rule is
when you create array, java initilizes its elements
to default values, no matter if this array is an instnce variable
or it is created within method and has local scope - this is the
important rule - it is good to keep it in mind while
programming. The way I tried to explain it in the last sentence
(>>>Once you create an Objeect you need to have all its instance variable
 initializaed.Now consider array elements as instance variables  of array object)
is really not that important - don't concentrate on that