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 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net