This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Val,
Thanks so much, you have been of great help to me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic