Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array as a local variable Initialization

 
Sanjay Mittal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I am reading all these posting and it is really helpful for my preparation. I am going to give test next Monday. Please help me to clear my confustion.
RHE books says Arrays are initialize when we declare them and it doesn't make any difference if they are local to a method.
If you compile code given below, it will give you compilation error.
public class test
{ public static void main(String args[])
int[] a, b[] ;
int c[][];
c = b; // Both are two dimensional
}
}
It will give compilation error that b is not intialized. I don't know why.
TIA
Sanjay
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjay,
An array is initialized when it's created (dimensioned) not when it's declared.
<pre>
public static void main(String[] args) {
int[] a, b[]; // declare arrays
int c[][]; // declare array
b = new int[3][3]; // create array
c = b; // compiles without error
}
</pre>
Hope that helps.
------------------
Jane
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic