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


Under Construction: We are in the process of updating and rewriting this lesson. Some sections are incomplete and are marked with [PENDING]. Continue to use our feedback form(in the Learning the Java Language trail) to tell us what you like and don't like about this lesson and the tutorial.

The Sort(in a .java source file) program shown below sorts the numeric values stored in an array(in the glossary), a data structure that can hold multiple values of the same type, and displays the sorted list. The program uses the bubble sort algorithm, a simple but inefficient algorithm, to sort the items. Running the Sort Program(in the Learning the Java Language trail) shows you how to run the program and the output to expect from it.
public class Sort {
    public static void main(String[] args) {
        int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076,
                              2000, 8, 622, 127 };

        for (int i = arrayOfInts.length; --i >= 0; ) {
            for (int j = 0; j < i; j++) {
                if (arrayOfInts[j] > arrayOfInts[j+1]) {
                    int temp = arrayOfInts[j];
                    arrayOfInts[j] = arrayOfInts[j+1];
                    arrayOfInts[j+1] = temp;
                }
            }
        }

        for (int i = 0; i < arrayOfInts.length; i++) {
            System.out.print(arrayOfInts[i] + " ");
        }
        System.out.println();
    }
}


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

Even a small program such as this uses many of the traditional language features of the Java programming language. This lesson describes variables, operators, expressions, and control flow statements.

Variables(in the Learning the Java Language trail)

Like other programming languages, Java allows you to declare variables in your programs. You use variables to hold data. All variables in Java have a type, a name, and scope.

Operators(in the Learning the Java Language trail)

The Java programming language provides a set of operators that you use to perform a function on one, two, or three operands.

Expressions, Statements, and Blocks(in the Learning the Java Language trail)

This section discusses how to combine operators and variables into sequences known as expressions. You will also learn how to construct statements and statement blocks.

Control Flow Statements(in the Learning the Java Language trail)

Programs use control flow statements to conditionally execute statements, to loop over statements, or to jump to another area in the program.

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