Java Reference
In-Depth Information
This statement contains a syntax error. The lack of a blank between the t in int and the
identifier a changes the reserved word int and the identifier a into a new identifier, inta .
The clarity provided by the rules of syntax and semantics frees you to adopt formats that
are pleasing to you and easier to understand.
The following example further elaborates on form and style.
2
EXAMPLE 2-27
Consider the following Java program:
//An improperly formatted Java program.
import java.util.*;
public class Example2_27A
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int num; double height;
String name;
System.out.print("Enter an integer: ");
num=console.nextInt(); System.out.println();
System.out.println("num: "+num);
System.out.print("Enter first name: ");
name=console.next();
System.out.println();System.out.print("Enter height: ");
height = console.nextDouble(); System.out.println();
System.out.println("Name: "+name);System.out.println("Height: "
+height);
}}
This program is syntactically correct; the Java compiler would have no difficulty reading
and compiling this program. However, this program is very hard for a human to read.
The program that you write should be properly indented and formatted. Next, we
rewrite the preceding program and properly format it.
//Properly formatted Java program.
import java. util.*;
public class Example2_27B
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int num;
double height;
String name;
Search WWH ::




Custom Search