Is a string in a Java program the same thing as a statement?

I am a Java noob. I used it for only a few days, and I'm still trying to figure it all out. In a program, is the string the same as the statement?

+6
source share
6 answers

Not. When compiling a program, the Java compiler does not consider lines, intervals, or other formatting problems. He just wants to see ; at the end of each statement . This line will work fine:

 int i = 13; i += 23; 

However, the implementation of such actions can, and most likely, cause problems with the readability of the source code. For this reason, this is not recommended.

It is also possible for a single statement to span multiple lines:

 int i = 13; 
+4
source

Not. I can write:

 int x = 1; int y = 2; 

This line and two statements.

+5
source

In a program, is a string the same as a statement?

Not.

Want to know the difference? Start with JLS & sect; 14.5: Blocks and statements :

 Statement: StatementWithoutTrailingSubstatement LabeledStatement IfThenStatement IfThenElseStatement WhileStatement ForStatement StatementWithoutTrailingSubstatement: Block EmptyStatement ExpressionStatement AssertStatement SwitchStatement DoStatement BreakStatement ContinueStatement ReturnStatement SynchronizedStatement ThrowStatement TryStatement StatementNoShortIf: StatementWithoutTrailingSubstatement LabeledStatementNoShortIf IfThenElseStatementNoShortIf WhileStatementNoShortIf ForStatementNoShortIf 
+4
source

According to the Java grammar :

 Statement: Block if ParExpression Statement [else Statement] for ( ForInitOpt ; [Expression] ; ForUpdateOpt ) Statement while ParExpression Statement do Statement while ParExpression ; try Block ( Catches | [Catches] finally Block ) switch ParExpression { SwitchBlockStatementGroups } synchronized ParExpression Block return [Expression] ; throw Expression ; break [Identifier] continue [Identifier] ; ExpressionStatement Identifier : Statement 

Based on this, you can easily see that a single statement can span multiple lines, and also a single line can span multiple statements. Also note that a statement is a very broad term.

+4
source

This line contains two statements:

 j = 5; j += 3; 

So, a string is not necessarily an expression ...

+2
source

Only in normal practice and for readability. In Java statements, they end with a semicolon, or in the case of blocks, in pairs of curlybraces ({}).

+2
source

Source: https://habr.com/ru/post/897573/


All Articles