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 Initialization of Local Arrays 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 "Initialization of Local Arrays" Watch "Initialization of Local Arrays" New topic

Initialization of Local Arrays

Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
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];
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

Joined: Jul 21, 2003
Posts: 522
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.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
Thanks Vicken i guess you are right.
I agree. Here's the link:
subject: Initialization of Local Arrays
It's not a secret anymore!