aspose file tools*
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
Author

Couple of basic syntax questions

Aaron Betts
Greenhorn

Joined: May 08, 2014
Posts: 1

Hi!
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
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23


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
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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: http://aspose.com/file-tools
 
subject: Couple of basic syntax questions