Lecture 1: Introduction

Balasubramanian Narasimhan
Department of Statistics
Stanford University
Stanford, CA 94305

Revision of Date

Table of Contents

Abstract

This is a highly simplified introduction to Java. The aim is to get you going. If you need further information, you can use the resources listed herein.

Introduction

[*]

Java is computing language developed by Sun Microsystems. Java compilers compile programs into bytecodes which are instructions that can be executed by a machine that is constructed in software. This is referred to as the java virtual machine (JVM). This is what qualifies Java to be called a platform-independent language. For, one only needs to construct a JVM for each platform; the Java programs can remain the same and no recompiling of programs is necessary.

Aside from the above benefit, Java combines a number of modern features that make it unique. Here are a few of them.

Java Tools

[*]

An important resource for all your Java needs is the Sun's Java Website (http://java.sun.com). Before you can do anything, you need to download Software Development Kits (SDK) for Java. SDK's contain java compilers and runtime libraries. Currently, you can download SDK's for almost all the major platforms, including Linux. In fact, I use the Linux SDK exclusively for all my development needs. For some packages, you need to become a member of the Java Development Connection, a free service.

One thing to be aware is that Java comes in different versions and even flavours, thanks to the Sun-Microsoft clash. In these lectures, I consider only Java version 1.2 and above, which is also known as the Java 2 platform. However, most of the examples here should work on 1.1 as well. If you plan to be writing applets, beware that versions of Java implemented in browsers among other things, can make programming a huge pain in the butt.

Also, we focus exclusively on Sun Java. Since Java is not a language embodied in an international standard as C and other languages are, Sun calls the shots. So does Microsoft, with respect to its own version, but the language is essentially the same. Still, these two can be incompatible and confusing if you don't know what you are doing. So my advice is to stick to the Sun version.

Please note that you need the SDK for development, not for running Java programs. For the latter, a runtime environment, also available from Sun will do.

Platform Help

[*]
Windows
The installation of Java SDK for Windows is pretty easy. Go to the Sun site and download stuff. Double click and go. Then update your path to find the java tools.

Linux Make sure you have glibc 2.x. Go to the Sun site and joint the Java Developer Connection. Then you can download the JDK with contributions of Blackdown and Inprise. This is an early access release.

Alternatively, if you can be satisfied with Java 1.1, I recommend IBM's blazing fast JDK 1.1.8. This is available from http://www.ibm.com.

Java Programs

[*]

Java programs are built from classes. The language itself looks very much like C. Here is a typical HelloWorld program in the two languages for comparison.

<HelloWorld in Java>=
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
    
}

<HelloWorld in C>=
#include <stdio.h>
void main(int argc, char **argv) {
  printf("Hello World!\n");
}

And you would compile and execute these programs as follows.

<Compiling and Running HelloWorld in Java>=
% javac HelloWorld.java
% java HelloWorld

<Compiling and Running HelloWorld in C>=
% cc -o HelloWorld HelloWorld.c
% HelloWorld

Ok, that suffices to give you a taste of things, but rarely will you be getting your hands that dirty when you have Emacs around. There is an excellent Java Development Environment (JDE) that you can use on both Unix and Windows. It is freely available from the url http://sunsite.auc.dk/jde. I will show you a simple example and then talk more about JDE later.

Some Simple Things to Remember

[*]

Java Programs as Wrappers

[*]

Many Java programs can be wrapped around existing programs to provide GUIs or nicer looks. For example, here is a program that is a graphical interface to the whois command.

Primitive types

[*]

Java has primitive data types and many of them are quite familiar to C programs. They are not the same as C, please note. Java has no pointer types.


boolean either true or false
char 16-bit Unicode character
byte 8-bit signed integer
short 16-bit signed integer
int 32-bit signed integer
long 64-bit signed integer
float 32-bit floating point (IEEE 754-1985)
double 64-bit floating point (IEEE-754-1985)
Primitive Types in Java [*]

Control Structures

Java has the if, if-else, for, while, switch, break constructs for controlling program flow. Some examples follow.

<If construct>=
if (p < 0.0){
   System.err.println("Probability " + p + " is < 0";
else if (p > 1.0) {
   System.err.println(p + " is a Texan probability I guess?"
} else {
   ...
}

<For construct>=
for (int i = 0; i < n; i++) {
  sum += a[i];
}

<While construct>=
while (<condition is true>) {
  ...
}

<Switch Statement>=
char type = fields[0].charAt(fieldOneLength - 1);
switch (type) {
case 'B':
case 'b':
    return new BookCitation(fields);
case 'J':
case 'j':
    return new JournalCitation(fields);
case 'P':
case 'p':
    return new ProceedingsCitation(fields);
case 'C':
case 'c':
    return new ComputerReadableCitation(fields);
case 'E':
case 'e':
    return new ElectronicJournalCitation(fields);
    /**
     * Future additions
     *  case 'D':
     *  case 'd':
     *     return new DissertationCitation(fields);
     *  case 'T':
     *  case 't':
     *      return new TechnicalReportCitation(fields);
     */
default:
    return new AdministrativeCitation(fields);
}

Named Constants

[*]

In C one uses #define directives to declare fixed constants. In Java, one uses the keywords static and final.

<Named Constant Example>=
static final STD_NORMAL_MEAN = 0.0;
static final double STD_NORMAL_STD_DEV = 1.0;

Classes and Objects

[*]

To create objects, one has to create a Class representing the object. The Class will be the type of the object. Here is an example.

<Point Class>=
public class Point {
        double abscissa;
        double ordinate;
}

<SimpleLinearRegressionModel Class>=
public class SimpleLinearRegressionModel {
  Dataset dataset;
  int responseIndex;
  int[] predictorIndices;
}

One creates objects with the new keyword.

Indices

[*]

Code Chunks

[*] This index is generated automatically. The numeral is that of the first definition of the chunk.

Index of Identifiers

[*] Here is a list of the identifiers used, and where they appear. Underlined entries indicate the place of definition. This index is generated automatically.