• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization of Local Arrays

 
Rashi Gulati
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am confused i have read in Khalid Mughal that local array's doesnot get intialized to default values, but while doing mock test i came across the question:
What will happen if you try to compile and run the following code?
public class Q
{
public static void main(String argv[])
{
int anar[]=new int[5];
System.out.println(anar[0]);
}
}
1) Error: anar is referenced before it is initialized
2) null
3) 0
4) 5
according to me the answer is 1 as anar is referenced before it is intialized but to my surprise the answer is 3 that is zero.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the contrary rashi, arrays are always initialized to their default values whether they where defined locally or globally. It seems like a typo error to me.
 
Rashi Gulati
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vicken i guess you are right.
Regards
Rashi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic