This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding the Array Declaration" Watch "Regarding the Array Declaration" New topic
Author

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
Greenhorn

Joined: Jul 27, 2007
Posts: 22
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]);
}

}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding the Array Declaration
 
Similar Threads
Problem in Arrays passing.
problem with initialization
Arrays
Simple problem
static final array