gudii9
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.
>>>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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.
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
>>>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
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
ASKER
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