This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why does not "int i;;" throw an error?  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y does not the statement int i;; give an error.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66182
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why (note that why is spelled "why" not "y") do you feel that it should?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because ";" is a line terminator and a blank line is valid in Java.

krishnadhar Mellacheruvu wrote:y does not the statement int i;; give an error.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why (note that why is spelled "why" not "y") do you feel that it should?


because empty line terminators can be used as code page fillers. It's just a thought. is it a valid one ?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:
because empty line terminators can be used as code page fillers. It's just a thought. is it a valid one ?


Not sure what you are saying ... are you implying that the extra semicolons should be an error because it can make your source code too long?

Henry
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Semicolon is not a line terminator.
It is a statement separator.

For example, this is legal Java code:
 
Tim Holloway
Bartender
Posts: 18704
71
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the semicolon is neither a line terminator nor a statement separator. It's a statement terminator. The difference between separated statements and terminated statements is whether or not the final semicolon is required. Pascal had statement separators, Java and C have statement terminators. Ruby and Javascript I think allow one to go either way.

The construct ";;" is a null statement. It does nothing. Null statements are allowable and even useful in some cases.

There are probably some cases in Java where they aren't allowed, but I cannot think of any offhand. However some IDEs will flag them as questionable when used in a context where there is no value to having a null statement.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Actually, the semicolon is neither a line terminator nor a statement separator. It's a statement terminator.

That's true. Thanks for correcting me.
Tim Holloway wrote:There are probably some cases in Java where they aren't allowed.

I've got one!But in this case there is nothing special about the empty statement. No statement is allowed before call to super().
 
Tim Holloway
Bartender
Posts: 18704
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that there are cases where the violation is less obvious, but you deserve a cow for cleverness!
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another one - the null statement is unreachable code.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the same as mine. ANY statement at this place is illegal. Nothing special about the empty one.

Please correct me if I'm wrong. I think that an empty statement is legal at any place where a statement is legal.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Please correct me if I'm wrong. I think that an empty statement is legal at any place where a statement is legal.

Well, apart from your own example, which would make that statement (no pun intended) need some qualification, I can think of one rather obscure case:

An empty statement is not allowed after a statement separator.

So, while:
  for (int i = 0, j = 0; i < somethingOrOther; i++) { ...
is valid
  for (int i = 0, ; i < somethingOrOther; i++) { ...
is not.

Winston
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have statements inside for statements (between parentheses). You have statement expressions there. So an empty statement is not allowed there because no statement is allowed there.
JLS wrote:14.14.1. The basic for Statement

The basic for statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false.
BasicForStatement:
   for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

(...)

ForInit:
   StatementExpressionList
   LocalVariableDeclaration

ForUpdate:
   StatementExpressionList

StatementExpressionList:
   StatementExpression {, StatementExpression}

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:You have statement expressions there. So an empty statement is not allowed there because no statement is allowed there.

Sounds like splitting hairs to me, because precisely the same rule applies whether inside a class, method, block, or loop:
  int i = 0, j = 0;
is legal:
  int i = 0, ;
is not.

Winston
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Paweł Baczyński wrote:You have statement expressions there. So an empty statement is not allowed there because no statement is allowed there.

Sounds like splitting hairs to me, because precisely the same rule applies whether inside a class, method, block, or loop:
  int i = 0, j = 0;
is legal:
  int i = 0, ;
is not.

I'd be willing to sub-split that particular hair and say that
is not legal for the same reason that
is not legal: it's not that the empty statement (authorized by JLS 14.6) can't follow a comma separator in that context; it's because only undeclared identifiers (in the given scope) can follow the comma separator in that context.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:I'd be willing to sub-split that particular hair and say that [...] is not legal...because only undeclared identifiers (in the given scope) can follow the comma separator in that context.

OK, I give up.

So in fact there is no such thing as a statement separator in Java; only a "declaration" separator, or a "statement expression" separator in for loops.

What about lambdas? Is the thing after a '->' called a statement or a statement expression, and is:
  x -> { ; }
allowed? Seems possible, but I can't test it because I'm still on v7.

Winston
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how it is called. Let me check JLS...

Ok, got it.

Lambda expression is defined as:
JLS wrote:LambdaExpression:
    LambdaParameters -> LambdaBody

Lambda body is:
JLS wrote:LambdaBody:
    Expression
    Block

So this part {;} is a block. You can put anything inside that you would put in any other block of statements.

For example:Which, of course, is the same as:That is, a runnable that does nothing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!