Forums Register Login

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

+Pie Number of slices to send: Send
y does not the statement int i;; give an error.
+Pie Number of slices to send: Send
Why (note that why is spelled "why" not "y") do you feel that it should?
+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
 

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 ?
+Pie Number of slices to send: Send
 

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
1
+Pie Number of slices to send: Send
Semicolon is not a line terminator.
It is a statement separator.

For example, this is legal Java code:
(1 cow) 2
+Pie Number of slices to send: Send
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.
(1 cow)
+Pie Number of slices to send: Send
 

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().
+Pie Number of slices to send: Send
I think that there are cases where the violation is less obvious, but you deserve a cow for cleverness!
+Pie Number of slices to send: Send
Here's another one - the null statement is unreachable code.
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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}

 
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 781 times.
Similar Threads
Primitive Casting
what is the difference between these two?
For Loop
Java Increment Operator pre/post fix
about supper() and this(..)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:04:07.