How to Punctuate Your Java Code - dummies

By Barry Burd

In English, punctuation is vital. Punctuation is also important in a Java program. The use of curly braces, semicolons, parentheses, double quotation marks, and periods tells Java how to read the code and makes the code easier for humans to understand.

This list lays out a few of Java’s punctuation rules:

  • Enclose a class body in a pair of curly braces.

    In this listing, the MyFirstJavaClass body is enclosed in curly braces.

    package org.allyourcode.myfirstproject;
    public class MyFirstJavaClass {
       * @param args
      public static void main(String[] args) {
                                       (null, "Hello");

    The placement of a curly brace (at the end of a line, at the start of a line, or on a line of its own) is unimportant. The only important aspect of placement is consistency. The consistent placement of curly braces throughout the code makes the code easier for you to understand. And when you understand your own code, you write far better code.

    When you compose a program, Eclipse can automatically rearrange the code so that the placement of curly braces (and other program elements) is consistent. To make it happen, click the mouse anywhere inside the editor and choose Source→Format.

  • Enclose a method body in a pair of curly braces.

    In the listing, the main method’s body is enclosed in curly braces.

  • A Java statement ends with a semicolon.

    For example, in the listing, the call to the showMessageDialog method ends with a semicolon.

  • A declaration ends with a semicolon.

    Again in the listing, the first line of code (containing the package declaration) ends with a semicolon.

  • In spite of the previous two rules, don’t place a semicolon immediately after a closing curly brace (}).

    The listing ends with two closing curly braces, and neither of these braces is followed by a semicolon.

  • Use parentheses to enclose a method’s parameters, and use commas to separate the parameters.

    In the listing (where else?) the call to the showMessageDialog method has two parameters: null and “Hello”. The declaration of the main method has only one parameter: args.

  • Use double quotation marks (“”) to denote strings of characters.

    In the listing, the “Hello” parameter tells the showMessageDialog method to display the characters Hello on the face of the dialog box.

  • Use dots to separate the parts of a qualified name.

    In the Java API, the javax.swing package contains the JOptionPane class, which in turn contains the showMessageDialog method. So javax.swing.JOptionPane.showMessageDialog is the method’s fully qualified name.

  • Use dots within a package name.

    The dots in a package name are a bit misleading. A package name hints at uses for the code inside the package. But a package name doesn’t classify packages into subpackages and sub-subpackages.

    For example, the Java API has the packages javax.swing,,, and many others. The word javax alone means nothing, and the package isn’t inside the package.

    The most blatant consequence of a package name’s dots is to determine a file’s location on the hard drive. For example, because of its package name, the code in the listing must be in a folder named myfirstproject, which must be in a folder named allyourcode, which in turn must be in a folder named org, as shown in this figure.