aspose file tools*
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
Author

Initialization of Local Arrays

Rashi Gulati
Ranch Hand

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

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.
Regards
Rashi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Initialization of Local Arrays