Java Reference
In-Depth Information
Nashorn will insert them automatically as it feels necessary. In some cases, the automatic
insertion of a semicolon may cause a misinterpretation of the program. The following
rules are used to insert a semicolon automatically:
A semicolon in automatically inserted if the parser encounters
source text that is not allowed in the statement currently being
parsed and the text is preceded by at least one line terminator
A semicolon is inserted after a statement if the statement is
followed by a closing brace ( } )
A semicolon is inserted at the end of the program
A semicolon is not inserted automatically if it will be parsed as
an empty statement (I will explain empty statements later in this
chapter). A semicolon is also not inserted in the header of a for
statement
Consider the following code:
var x = 1, y = 3, z = 5
x = y
z++
printf("x = %d, y = %d, z = %d", x, y, z)
The parser will automatically insert semicolons as statement terminators as if you
have written the code as follows:
var x = 1, y = 3, z = 5;
x = y;
z++;
printf("x = %d, y = %d, z = %d", x, y, z);
Consider the following code:
var x = 10, y = 20
if (x > y)
else x = y
The parser will insert a semicolon after the variable statement and the assignment
statement x = y , but not after the if statement. The transformed code will be as follows:
var x = 10, y = 20;
if (x > y)
else x = y;
A semicolon is not automatically inserted after the if statement because the inserted
semicolon will be interpreted as an empty statement. This code will not compile because
the if statement has a condition but a statement is missing.
 
Search WWH ::




Custom Search