Flylib.com

Books Software

 
 
 

General Syntax and a Simple Java Program


General Syntax and a Simple Java Program

Java is based on C and C++ and much of its basic syntax is similar to those languages. This is why the transition from a language like C or C++ to Java is easier than the transition from a language like Fortran to Java. For example, every executable statement in a Java program is terminated with a semicolon just as in a C or C++ program. Java array indices start at 0 and the array access expression uses brackets ”[ ] ” rather than parentheses ”( ).

Lines of code can be indented any way you like. You can start a line in the first column or the 20th if you desire . In Java, indenting is primarily used to make certain sections of a code listing stand out, making it easier to read. It is customary, for instance, to indent the body of a method from the method declaration.

Braces ”{ } ”denote blocks of code in Java. You can define blocks of code anywhere in your program. Typically blocks of code are used to designate class definitions, method bodies, and loop and control structure elements.

Before we go any further, let's look at a simple Java program that will contain many of the elements we discussed in the previous paragraphs.

Example: A Simple Java Program

This is the simplest type of Java program. It consists of one class named SimpleProgram that defines one method called main() . Your programs will eventually have a lot more to them. You may import packages, extend the capabilities of existing classes, define constructors and methods , and so on. But every Java application you will write will consist of at least one class and will define a main() method.

The program defines two blocks of code. The first encloses the definition of the SimpleProgram class. The second surrounds the body of the main() method. Note the indentation. There are two levels of indentation. The first distinguishes the main() method from the SimpleProgram class. The second helps the body of the main() method stand out from the method declaration syntax.

Inside the main() method, two variables are declared and one is given an initial value. A mathematical operation is performed and the value of the area variable is assigned to the result. The println() method is called to write a String of text to an output stream. In this case the main() method calls the println() method of the standard output stream, and the String is written to the console. Basic printing is described at the end of this chapter, and the Java I/O classes are described in detail in Chapter 25.

public class SimpleProgram
{
  public static void main(String args[]) {
    double area, radius = 2.0;
    area = Math.PI*radius*radius;
    System.out.println("area of circle is " + area);
    }
}

Output ”

area of circle is 12.566370614359172

Comments

Every good computer program needs comments to help someone (even the developer) figure out what the code is doing. Java provides three types of comments. The first is the C-based multiline comment. The comment begins with the token /* and ends with the token */ . Everything between these characters is considered a comment and is ignored by the compiler. As the name suggests, multiline comments can span more than one line.

The second type of comment is a single line comment. The token // is placed at the beginning of the comment. This comment cannot span multiple lines. The third type is a documentation comment. The syntax is similar to a multiline comment except it begins with /** . There is a utility that comes with the Java SDK called javadoc . By default, this utility creates HTML files that describe Java source code. (The Sun Java doc pages were created using the javadoc utility.) Documentation comments are incorporated into the HTML files created by javadoc .

Example: Using Comments

public class CommentDemo
{
  public static void main(String args[]) {
    //  This is a single line comment
    /*  This is a comment
        that can span more
        than one line  */
    /**  @author Grant Palmer */
  }
}

This example shows the three types of comments in action. The @author syntax is one of the special javadoc tags that can be used with documentation comments. For more information on documentation comments and their associated tags, consult the Sun Java online documentation at http://java.sun.com/j2se/javadoc.