| 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. <br />- What truth? <br />- That there is no spoon!!!
|
 |
Rashi Gulati
Ranch Hand
Joined: Jan 08, 2004
Posts: 44
|
|
Thanks Vicken i guess you are right. Regards Rashi
|
 |
 |
|
|
subject: Initialization of Local Arrays
|
|
|