Dought in Array
Hi All,
I am newbie to java,i was doing a simple program, i was going through arrays , i am getting an error message .
class arraydemo
{
publicstaticvoid main(String[] args)
{
int arr[] =newint[5];
for(int i = 0;i < 5;i++){
arr[i];// I think this line is correct,
}
}
}
if i declare arr[i];// i get error as "Not A Statement."
if i declare arr[i] = 0;// no error occurs
please can u tell me what is the difference and my mistake
Thanks
o:)
> Hi All,
>
> I am newbie to java,i was doing a simple program, i
> was going through arrays , i am getting an error
> message .
>
> > class arraydemo
> {
> public static void main(String[] args)
> {
> int arr[] = new int[5];
> for(int i = 0;i < 5;i++){
> arr[i]; // I think this line is correct,
> }
> }
> }
>
> > if i declare arr[i]; // i get error as "Not A
> Statement."
>
> > if i declare arr[i] = 0; // no error occurs
>
>
> please can u tell me what is the difference and my
> mistake
>
> Thanks
> o:)
arr[i]; //is not statement
arr[i] =0; //is statement
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#5984
Are you coming from a C background? In that language you can turn any
expression into a statement by tacking on a semicolon:
a>b;
That's an easy syntax rule to remember and to implement in a parser,
but it's also rather silly. The corresponding rule in Java is given in the [url=http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.8]JLS section 14.8[/url]:
Certain kinds of expressions may be used as statements by following them with semicolons:
(*) Assignment: x = 0;
(*) PreIncrementExpression: ++x;
(*) PreDecrementExpression: --x;
(*) PostIncrementExpression: x++;
(*) PostDecrementExpression: x--;
(*) MethodInvocation: obj.f(x);
(*) ClassInstanceCreationExpression: new X();
And that's it. Notice that arr[i] isn't on the list!
Hi p_epi , DrLaszloJamf ,Thank u very much for your replies,I am not from a c background, i am learning java programming now,i will look in the links and learn about statements.Thank u once againo:)