Java Syntax
Java can be thought of as a sequence of statements with each statement ending with a semi-colon.
Comments
Everything after a double slash // is a comment.
Multi-line comments start with:
/*
and end in
*/
Blocks
Statements surrounded by curly brackets form a block. Blocks can be nested.
Example
/*
Author: Ben
The HelloWorld class implements an application that prints out "Hello World".
*/
public class HelloWorld {
// Main Method
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The large block is a class definition which is named HelloWorld
.
The main
block is an actionable member of that class.
The class name must be the same as the name of the source code file in which it is defined (but with the extension .java
Java Keywords
There are some words that are reserved for the compiler can cannot be used for tother purposes such as variable names.
Main Methods
HelloWorld
has only one method called main
.
- A class with a
main
method like this is called the application class. - The
main
method is the entry point of the application, from which the JVM begins the execution of the program.
Capitalisation and Indentation
- Java is case sensitive so
main
is different toMain
. - Indentation carries no meaning. The lines are ended by
;
.
Code Conventions
- Identifiers use
CamelCase
. Class names start in capitals; method and variable names in small letters. - Indent your code when nesting.
- Add comments to explain what your code does.