File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding the Array Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding the Array Declaration" Watch "Regarding the Array Declaration" New topic

Regarding the Array Declaration

Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
Hi friends,

In K&b,chapter 3 age no:196.
Array element are always given default values,regardless of where the array itself is declared or instatiated.For me this line is confusing so i wrote a program

here i am getting a compile time exception saying array is not intialised

J 4 Java
B Misra

Joined: Jul 27, 2007
Posts: 24
An array is first 'declared' (i.e. declaring the reference only)
then it is 'constructed' that is an array object is created in heap.
At this point The elements of the constructed array are initialized to their default values.

At next step you can intialize/ set values for each element of the array.

in you sample code if you construct the array then you'll see that the element values are initialized to default 0 (for primitive int)

public class ArrayTest {

/** Creates a new instance of ArrayTest */
public ArrayTest() {
public static void main(String args[]){
int arr[]= new int[5];//declared here
for(int i=0;i<5;i++)
System.out.println("Array Values --"+arr[i]);

I agree. Here's the link:
subject: Regarding the Array Declaration
It's not a secret anymore!