Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array initialization

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>>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
Marshal
Pie
Posts: 21371
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>>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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic