This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

array initialization

 
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.
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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:
 
Marshal
Posts: 65074
247
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!