File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Couple of basic syntax questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Couple of basic syntax questions" Watch "Couple of basic syntax questions" New topic

Couple of basic syntax questions

Aaron Betts

Joined: May 08, 2014
Posts: 1

Just diving into Java and am working through the exercises in Head First Java.

Couple of basic questions:
1. methods can be nested right?
2. I've learned that statements always have to end with a semi-colon, but this piece of code doesn't. Why? while (x < 5) {

Here is the source code (for context) with my questions in the comments.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

1. No.

2. (a) because the while statement (it's a statement, not a method) doesn't end until line 12, and (b) blocks do not need to be terminated with a semicolon.

If you used better indentation, the structure might be more apparent. Give it a try.

[Asking smart questions] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

This is not a "nested method".

You can have a block of code enclosed in { and }. The while statement needs a block of code, the body of the loop.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
Welcome to the Ranch

As you have already been told, you have only written the first part of the while loop; it must be followed by a single statement but you can susbtitute a block for that single statement. It is all in the Java Language Specification (JLS); here is the definition for Statement:You can see that while (line 10) is a kind of statement, and most but not all kinds of statement finish with semicolons. Block does not require a terminal semicolon, which would actually count as a new Statement. The JLS is by no means easy to understand, I am afraid.
I agree. Here's the link:
subject: Couple of basic syntax questions
It's not a secret anymore!