The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search

Trail: Learning the Java Language

Lesson: The Nuts and Bolts of the Java Language

The Count class shown below contains a method named countChars that reads and counts characters from a Reader (an object that implements an input stream of characters) and then displays the number of characters read. Even a small method such as this uses many of the traditional language features of Java and classes from the Java API.
import java.io.*;
public class Count {
    public static void countChars(Reader in) throws IOException
    {
        int count = 0;

        while (in.read() != -1)
            count++;
        System.out.println("Counted " + count + " chars.");
    }
    // ... main method omitted ...
}

Through a line-by-line investigation of this simple method, this lesson describes Java's traditional language features such as variables and data types, operators, expressions, control flow statements, and so on.


Impurity Alert:  Using System.out is not 100% Pure Java because some systems don't have the notion of standard output.

The previous listing of the Count class omits the main method needed to try the countChars method. Running the countChars Method shows you the main method and how to run it.

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 these two variables among others:

Operators

The Java language provides a set of operators which you use to perform a function on one or two variables. The countChars method 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 is achieved through expressions.

Control Flow Statements

The while statement in countChars 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.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail Search