Java statements and instructions

Jayesh Sonkusare :

I read that all Java statements end with a semicolon. But i've also noticed many times on the internet that

public static void main

has been called a statement although it doesn't have a semicolon. So, is there a definite term for statement? Is there any difference between statement and instruction in Java?

Stephen C :

I read that all Java statements end with a semicolon.

That is incorrect. The following Java statements do NOT need to end with a semicolon:

  • if statements
  • some loop statements (for, while)
  • synchronized statements
  • try statements
  • block statements; i.e. { ... }

The thing that these all have in common is that they end in a subsidiary statement which may be either a block or not; e.g.

if (a) this.b();

versus

if (a) { this.b(); }

But i've also noticed many times on the internet that public static void main has been called a statement although it doesn't have a semicolon.

That is not a statement. It is a part of a declaration. (A method declaration.)

So, is there a definite term for statement?

The Java Language Specification says this:

"The sequence of execution of a program is controlled by statements, which are executed for their effect and do not have values."

So from that we can extract the following loose definition: A statement is a Java language construct that is executable, is executed for its effect, and doesn't have a value1.

However, I think that a better way to see the difference between a statement and a non-statement in Java is to list the different kinds of statements.

  • Start with the list above.
  • Add the following:
    • local variable declarations (with or without initializers)
    • calls to methods, method references and lambdas
    • new statements
    • assignment statements
    • prefix / postfix increment and decrement expressions
    • do ... while statements.
    • continue and break statements
    • throw statements
    • return statements
    • empty statements; e.g. the second ; in a = b; ;

Not-statements (declarations) include the following:

  • A class declaration
  • An interface declaration
  • An enum declaration
  • A field declaration
  • A method declaration
  • A constructor declaration
  • A package declaration
  • An import declaration
  • A module declaration
  • An initializer block.

(Some people call package, import and module declarations "statements", but that is not what the JLS calls them, and that doesn't fit the definition above.)

Is there any difference between statement and instruction in Java?

The term "instruction" doesn't have any technical meaning in Java. However, a Java statement is a form of instruction to the program to do something.


1 - The "doesn't have a value" is a bit of a stretch. For example, a statement such a i++; does produce a value ... but we are ignoring it. However, the above is a direct quotation from the JLS, albeit that it is descriptive rather than a serious attempt to define "statement".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=309605&siteId=1