Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array" Watch "array" New topic
Author

array

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
class Tester {
public static void main(String argv[]){
int anar[]=new int[]{1, 2, 3, 5};
System.out.println(anar[2]);
}
}
In this case I got the output of "3".
But in the down code does it mean that the array of int is never initialized .
class Tester {
public static void main(String argv[]){
int anar[]=new int[5];
System.out.println(anar[0]);
}
}
This prints an output of "0" even when the anar[2] is changed to input of two. I thought that it will get the value of 5. Please can anyone explain for me why I got this.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
when you declare and construct an array all of its components will be initialized to their default values (which depend on the type of the components). In this case, writing

will declare, construct and initialize all components to the default value "0". The array looks like this {0,0,0,0,0}. So printing anar[0] (or whatever index) will print "0".
HIH
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Val,
Thanks so much, you have been of great help to me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array