Plz.. help me out to understand the java concepts regarding arrays
when i write the code:
int a;
System.out.println(a);
compiler flags an error that a might not have been initialized.
but when i write this
int [] a= new int[10];
System.out.println(java.util.Arrays.toString(a));
compiler doesnot flag any error
and gives a output
[0,0,0,0,0,0,0,0,0,0]
I want to understand from where the integers stored in the array are getting initial value 0 without any expilicit initialization.
Plz.. help me out i am a beginner in java .
Re: Plz.. help me out to understand the java concepts regarding arrays
That's just the default value populated in an array- 0 for ints, null for Objects. It's similar to how class variables (not method variables) get a default value, even if you don't explicitly give them one.
Re: Plz.. help me out to understand the java concepts regarding arrays
Quote:
Originally Posted by
KevinWorkman
That's just the default value populated in an array- 0 for ints, null for Objects. It's similar to how class variables (not method variables) get a default value, even if you don't explicitly give them one.
thanks a lot kevin for ur help.
Re: Plz.. help me out to understand the java concepts regarding arrays
Quote:
Originally Posted by
Arindam Sarkar
thanks a lot kevin for ur help.
No problem. Keep in mind that had you done this instead:
Code java:
int [] a; //no declaration!
System.out.println(java.util.Arrays.toString(a));
...you would have received a similar error.