leronvane
leronvane

Reputation: 49

Is a block a statement in Java?

This could apply to other languages but asking specific to Java. In Java would a block itself be a statement? In the Java tutorials: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html a block is treated separately from a statement by saying it is a sequence/group of statements but does not say it is a statement itself. Can someone please also explain why.

Upvotes: 4

Views: 108

Answers (2)

rzwitserloot
rzwitserloot

Reputation: 102902

See JLS §14.5 (see other answer). Also, in both ecj and javac (the two most popular java parsers/compilers out there):

  • Javac's AST node representing a block: JCBlock, the AST node that represents a block, extends JCStatement.
  • ecj's: Block extends Statement.

So, the spec says it is a statement, and both major implementations represent it as a subtype of its version of Statement.

Upvotes: 2

kaya3
kaya3

Reputation: 51034

Yes, according to the grammar defined in the Java Language Specification (§14.5), a Block is a kind of Statement, specifically it is a StatementWithoutTrailingSubstatement.

Upvotes: 5

Related Questions