Previous | Next | Trail Map | Writing Java Programs | Table of Contents


The Nuts and Bolts of the Java Language

The following application reads and counts characters from an input source and then displays the number of characters read. Even a small program such as this uses many of the traditional language features provided by Java and several classes in the libraries shipped with the Java environment.
class Count {
    public static void main(String[] args)
        throws java.io.IOException
    {
        int count = 0;

        while (System.in.read() != -1)
            count++;
        System.out.println("Input has " + count + " chars.");
    }
}
To Run the Character-Counting Application you must enter characters at the keyboard.

Through a line-by-line investigation of this simple Java application, this lesson describes Java's traditional language features such as control flow statements, data types, variables, expressions, operators, and so on. You will also be introduced to some of Java's other features: the main method, exceptions, and standard input and output.

Variables and Data Types

Like other programming languages, Java allows you to declare variables in your programs. You use variables to contain data that can change during the execution of the program. All variables in Java have a type, a name, and scope. The example program shown previously declares two variables:

Operators

The Java language provides a set of operators which you use to perform a function on one or two variables. The character-counting program uses several operators including the ones shown here in bold:

Expressions

Operators, variables, and method calls can be combined into sequences known as expressions. The real work of a Java program, including the character-counting example, is achieved through expressions.

Control Flow Statements

The while statement in the character-counting program is one of a group of statements known as control flow statements. As the name implies, control flow statements control the flow of the program. In other words, control flow statements govern the sequence in which a program's statements are executed.

Arrays and Strings

Two important data types in any programming language are arrays and strings. In Java, both arrays and strings are full-blown objects. Other languages force programmers to manage data in arrays and strings at the memory level using direct pointer manipulation. The character-counting program defines an array of strings.

Other Features of the Character-Counting Application

In addition to the traditional programming language features highlighted in the sections above, the character-counting program introduces several other features with which you will become familiar over the course of this tutorial: the main method, exceptions, and standard input and output.


Previous | Next | Trail Map | Writing Java Programs | Table of Contents