• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Rules Round #154

 
Win jones
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question )
Assuming all variables are declared and initialized properly will the following code compile
for (i = 1; k <5;j++){};

why is there a semi-colon after the last curly brace
because of that the code will not compile
 
Mukesh Poddar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As your Question itself says ...all the variables i,j,k are declared and initialized properly.....
The program will run Succesfully.....

Since in Java an ; separates the One statement from the other......

And the Bracket before the "{}" dosen't signify anything it just gives compiler the infiormation that a new block is started....

so te compiler will treate as a null statement.... and compile the program succesfully.....
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just tell me that
whether
;
statement in java program will not cause compiler error ?
just asking...

 
Marilyn de Queiroz
Sheriff
Posts: 9061
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amit taneja:
just tell me that
whether
;
statement in java program will not cause compiler error ?

An extra semicolon after the curly brace will not cause a compiler error.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amit taneja:
just tell me that
whether
;
statement in java program will not cause compiler error ?
just asking...



The semicolon designates an empty statement:

// OK
class Foo {
void bar() {}
int x;
}

// OK
class Foo {
void bar() {}
;
}

// OK
class Foo {
void bar() {};
}

// OK
class Foo {
;;;;;;;;;;;void bar(){;;};;;;;;;;;;;;;;;;;;;;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic