GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes array initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array initialization" Watch "array initialization" New topic
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: 18878
    
  40

>>>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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14196
    
  20

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 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array initialization