Basic Java Syntax

Contact Us or call 1-877-932-8228
Basic Java Syntax

Basic Java Syntax

General Syntax Rules

Java is case-sensitive. main(), Main(), and MAIN() would all be different methods.

There are a limited number of reserved words that have a special meaning within Java. You cannot use these words for your own variables or methods.

Some examples of reserved words are:

  • public
  • void
  • static
  • do
  • for
  • while
  • if

Most keyboard symbol characters (the set of characters other than alphabetic or numeric) have a special meaning.

Names may contain alphabetic characters, numeric characters, currency characters, and connecting characters such as the underscore ( _ ) character:

  • Names may not begin with a numeric character.
  • Note that the set of legal characters draws from the entire Unicode character set.
  • Also note that it is probably impossible to write a succinct set of rules about what are valid characters, other than to say a character, that when passed to Character.isJavaIdentifierPart(char ch), results in a true value.

The compiler parses your code by separating it into individual entities called tokens or symbols in computer science jargon:

  • Names (of classes, variables, and methods).
  • Command keywords.
  • Single or compound symbols (compound symbols are when an operation is signified by a two-symbol combination).

Tokens may be separated by spaces, tabs, carriage returns, or by use of an operator (such as +, -, etc.).

Since names may not contain spaces, tabs, carriage returns, or operator characters, these characters imply a separation of what came before them from what comes after them.

Extra whitespace is ignored. Once the compiler knows that two items are separate, it ignores any additional separating whitespace characters (spaces, tabs, or carriage returns).

Java Statements

  • A Java statement is one step of code, which may span across multiple lines.
  • Java statements end with a semicolon (the ; character).
  • It is OK to have multiple statements on a single line.
  • Program execution is done statement by statement, one statement at a time from top to bottom (if there is more than one statement on a line, execution goes from left to right).
  • Within a statement, execution of the individual pieces is not necessarily left to right. There are concepts called operator precedence and associativity that determine the order of operations within a statement.

Blocks of Code

A block of code:

  • Is enclosed in curly braces - start with { and end with }.
  • Consists of zero, one, or more statements.
  • Behaves like a single statement to the outside world..
  • May be nested (e.g., it may contain one or more other blocks of code inside).
  • Generally belong to whatever comes before it. Although it is perfectly OK to create a block that does not, this is almost never done.

A complete method is a single block of code, most likely with nested blocks.

The diagram below illustrates how blocks of code can be nested: Nested blocks example

If you want, go ahead and modify your Hello World program to match this example.


A comment:

  • Is additional, non-executable text in a program used to document code.
  • May also be used to temporarily disable a section of code (for debugging).

Block Comments

Block comments are preceded by /* and followed by */.

Some rules for block comments:

  • May not be nested.
  • May be one or more lines.
    /* this is a block comment
    * asterisk on this line not necessary, but looks nice
  • May span part of a line, for example, to temporarily disable part of a statement:
    x = 3 /* + y */ ;

Single-line Comments

A single line can be commented by preceding the comment with two forward slashes: //. Note that:

  • The comment ends at the end of that line.
  • Single-line comments may be nested inside block comments.
    y = 7;
    * temporarily disable this line
    * which has a comment to end of line
    x = 3 + y; // add 3 for some reason

Java specifies a third type of comment, the javadoc comment:

  • Java contains a self-documentation utility, javadoc, which builds documentation from comments within the code.
  • javadoc comments begin with /** and end with */.
  • Comments only work as javadoc comments when placed at specific locations in your code (immediately above anything documentable - the class itself and its members), otherwise they are treated as ordinary comments.
    * Represents a person, with a first name and last name.
    public class Person {
    	/** The person's first name */
    	public String firstName;
    	/** The person's last name */
    	public String lastName;