I'm a Java noob. I've only used it for a few days and I'm still trying to figure it all out. In a program, is a line the same thing as a statement?
-
So many answers... Which one do I accept? – Benny Sep 18 '11 at 17:24
-
Accept the one that works for you. – fireshadow52 Sep 18 '11 at 17:25
-
1The one that is most helpful to you. If they are all equally helpful, typically the first answer posted is accepted. – Matt Ball Sep 18 '11 at 17:25
-
2@Benny Make a program and use `Random` to let the program chooses the one you will accept ;) – Eng.Fouad Sep 18 '11 at 17:25
-
Accept the oldest edited answer which most closely clears your doubt. – Hari Menon Sep 18 '11 at 17:26
-
LOL. @Matt Ball Which one was posted first? – Benny Sep 18 '11 at 17:27
-
@Benny hover your mouse over the "answered X mins ago" to see the timestamp. I think it was mine... – Matt Ball Sep 18 '11 at 17:28
-
@Matt Ball I've done that, and it is indeed your answer. However, I'm now debating between fireshadow's answer and yours. I sure wish I could accept two... :( – Benny Sep 18 '11 at 17:33
-
@Matt Ball I've accepted fireshadow's answer, but as soon as I can upvote I will upvote yours. :) – Benny Sep 18 '11 at 17:41
-
Voting up requires 15 rep: http://stackoverflow.com/privileges/vote-up ...there you go `:)` – Matt Ball Sep 18 '11 at 17:42
-
@Matt Ball Is there a bug in the system? It says `Please login or register to vote for this post` and doesn't let me upvote. :( – Benny Sep 18 '11 at 17:48
-
3let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3576/discussion-between-matt-ball-and-benny) – Matt Ball Sep 18 '11 at 17:51
6 Answers
No. I can write:
int x = 1; int y = 2;
That's one line, and two statements.

- 95,302
- 53
- 242
- 374
In a program, is a line the same thing as a statement?
No.
Want to know the difference? Start with the JLS §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

- 354,903
- 100
- 647
- 710
No. The Java compiler doesn't look at lines, spacing, or other formatting issues when compiling a program. It just wants to see the ;
at the end of each statement. This line would work just fine:
int i = 13; i += 23;
However, doing things like this can--and most likely will--cause readability issues with the source code. For this reason, it isn't recommended.
It is also possible for a single statement to span multiple lines:
int i =
13;

- 6,526
- 3
- 34
- 62

- 6,298
- 2
- 30
- 46
According to 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 one statement can span multiple lines but also single line can host multiple statements. Also note that statement is a very broad term.

- 334,321
- 69
- 703
- 674
This line includes two statements:
j = 5; j += 3;
So, a line is not necessarily a statement...

- 13,680
- 3
- 46
- 47
Only by common practice, and for readability. In Java statements are terminated with semicolons, or in the case of blocks, by pairs of curlybraces ( { } ).

- 6,914
- 2
- 23
- 31