Basic Language Syntax

This starts looking at the basic syntax or grammar of the Java language. We use an example program to illustrate language basics such as variables, Java keywords, data types, and arithmetic operations. We discuss topics related to program flow control.

Throughout this we use an example application, Circle, which calculates the area of a circle. The radius is input as a parameter to the program.

/* this application reads in a radius of a circle and outputs its area. */

public class Circle
{
static final double PI = 3.14159;
public static void main(String[] args)
{
int radius;
double area;
radius= Integer.parseInt(args[0]);
// area formula
area = PI * (radius * radius);
System.out.println(''A circle of radius " + args[0]
+ '' has area of " + area);
}
}


An example of the output of Circle is as follows:

> java Circle 5


A circle of radius 5 has area of 78.53975

No comments:

Post a Comment