Introduction to Java Programming

The Context of Java

Java is a Programming Language

The Heritage of Java

The Architecture of Java

Elements of a Java Program

The basic working elements of a Java program are statements organized into methods, which are grouped into classes. A collection of classes make up a Java program.

Here is a short example program that we will be studying in greater detail later in these notes.

/*
 First example program for CMSC 150.
 */

package ftoc;

public class TempConverter {

    public static void main(String[] args) {
        double fTemp; // Fahrenheit temperature
        double cTemp; // Centigrade temperature
        String message;
        
        fTemp = 68.0; // Fahrenheit room temperature
        
        cTemp = (fTemp - 32.0)*5/9;
        
        message = "68 degrees F = " + cTemp + " degrees C.";
        
        System.out.println(message);
    }

}

The source code you see above goes into a Java source code file named "TempConverter.java". Java uses a highly structured system of naming in which the name of the source code file must match the name of the class defined in that file. To run the program shown above, we will use our development environment to compile the Java source code into a Java class file named "TempConverter.class" that can be run in the Java runtime environment.

Java uses curly brace pairs to group items and organize them in a heirarchy. The outermost pair of braces in this example mark off the boundary of the TempConverter class. The TempConverter class contains a single method called the main method. The inner pair of curly braces mark the bounds of the body of the main method.

Every Java program has to have a main method that will act as the starting point for the program when the program runs. When the program runs, Java will execute the statements in the body of the main method in the sequence shown above. When the last statement is executed, the program terminates.

Comments

The Java compiler treats everything on a line following the // combination as a comment and ignores it when compiling a program. Likewise, anything appearing inside a /* ... */ pair is treated as a comment. Programmers put comments in programs to document what various parts of a program do.

Statements

The body of a Java method is made up of a list of statements. Statements come in two basic varieties, simple statements and compound statements. Today we will only see simple statements. The first special punctuation rule that applies to statements is that simple statements must all end in a semicolon in much the same way that english sentences must end a period.

The semicolon punctuation is necessary because formatting rules for Java programs are rather loose. For example, a single statement can be spread over several lines or several statements can appear on a single line. Without the semicolon to act as punctuation, the Java compiler would have a hard time knowing where one statement ends and the next begins.

Variables

Computer programs exist to process and manipulate information. The basic unit of information in a Java program is the Java variable. Like a variable in mathematics, a Java variable represents a single item of data that can be manipulated. Unlike mathematical variables, which usually represent numerical quantities, Java variables can represent a wide array of different kinds of information, such as numbers, text, sounds, and even rich data such as feature length motion pictures.

The Java language requires that all variables be declared before being used. A variable declaration consists of a type and a variable name. The type specifies what kind of information this variable will hold, while the name identifies the variable. In the first few examples we see we will only be using three basic data types, the double, the int, and the String. The int is used to store integer values. The double is used to store floating point numbers. String variables are used to store text.

Here are two examples of simple variable declaration statements.

int x;
double y;

The first statement declares an integer variable named x, while the second declares a floating point variable named y.

Declaring a variable does not give it a value. To give a variable a value, you must initialize it with an assignment statement. This can be done in the same statement as the declaration or separately, as this example shows.

int x = 2;
double y;
y = 3.5;

You can also assign the value of one variable to another.

double pi = 3.14159;
double omega = pi;

String variables are typically initialized with text enclosed in double quotes.

String greeting;
greeting = "Hello, I am a String";

Arithmetic

You can use Java statements to do simple arithmetic. Here are some rules that govern the behavior of arithmetic in Java.

String Concatenation and Automatic Type Conversion

String variables are designed to hold text. The simplest way to create some text is to put some text in quotes. This works fine if you know exactly what you want the text to say. In many other cases you will not know in advance what the text should say and you will have to construct an appropriate text string at run time. The most common scenario in which this happens is when you want to display the value of a variable as the program runs. The example program above computes a Centigrade temperature value and stores that in the variable named cTemp. To display that numerical value, we have to have a method that will convert the number into text. Here is the Java code that does the conversion:

message = "68 degrees F = " + cTemp + " degrees C.";

Two things are happening in this statement. The first is that we are assembling some text by concatenation, or gluing together several pieces of text. Java uses the '+' operator to do text concatenation. One of the items we are concatenating here, cTemp, is a variable of type double containing a numerical value. Java does an automatic type conversion to convert the numerical data stored in the cTemp variable into text before doing the text concatenation. The end result here is that the String variable message ends up containing a block of text made up of the pieces specified here.

Text Output

For a program to be useful, it has to have some mechanism for displaying the results it computes. Java offers several mechanisms for displaying output. The simplest mechanism looks something like this:

System.out.println(message);

When this statement gets executed as the program runs in NetBeans, the text contained in the message variable will be displayed in the output pane at the bottom of the NetBeans window.