| Author |
array initialization
|
Mathew Lee
Ranch Hand
Joined: Jun 08, 2009
Posts: 238
|
|
>>>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 following 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.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
>>>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 am guessing that this paragraph has been taken out of context -- because I couldn't really understand it either.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
It means that no matter where you create the array (as a member variable, local variable, ...), the elements of the array will always be initialized to the default value of the element type. For example:
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Jesper de Jong wrote: . . . initialized to the default value of the element type. . . .
And here you can see the default values; it includes "array" there.
|
 |
 |
|
|
subject: array initialization
|
|
|