• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
y does not the statement int i;; give an error.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why (note that why is spelled "why" not "y") do you feel that it should?
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Semicolon is not a line terminator.
It is a statement separator.

For example, this is legal Java code:
 
Saloon Keeper
Posts: 26279
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 26279
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that there are cases where the violation is less obvious, but you deserve a cow for cleverness!
 
Marshal
Posts: 4007
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another one - the null statement is unreachable code.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
For my next trick, I'll need the help of a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic