Computer Science Building, Princeton University


Introduction to CS


1.  A Simple Machine

2.  Java Programming

    • Hello World

    • Primitive Types

    • Conditionals, Loops

    • Input and Output

    • Arrays

    • Functions

    • Recursion

3.  OOP

4.  Data Structures

5.  A Computing Machine

6.  Building a Computer

7.  Theory of Computation

8.  Systems

9.  Scientific Computation

10.  Perspective


 Lecture Notes

Assignments

FAQ









2.1. YOUR FIRST JAVA PROGRAM:   HELLO WORLD


In this section, our plan is to lead you into the world of Java programming by taking you through the three basic steps required to get a simple program running. The Java system is a collection of applications not unlike any of the other applications that you are accustomed to using (such as your word processor, e-mail program, or internet browser). As with any application, you need to be sure that Java is properly installed on your computer. You also need an editor and a terminal application. Here are system specific instructions for three popular home operating systems: Windows, Mac OS X, Linux.

Programming in Java.

We break the process of programming in Java into three steps:
  1. create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java
  2. compile it by typing "javac MyProgram.java" in the terminal window
  3. run (or execute) it by typing "java MyProgram" in the terminal window

The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named MyProgram.class); the third actually runs the program. It is worthwhile for you to create, translate, and run a sample program. Once you have done so, you can move on to more interesting programs.

Creating a Java program.

A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail. HelloWorld.java is an example program. Type these character into your text editor and save it into a file named HelloWorld.java.
public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello, World");
   }
}

Compiling a Java program.

At first, it might seem to you as though the Java programming language is designed to be best understood by the computer. Actually, to the contrary, the language is designed to be best understood by the programmer (that's you). The computer's language is more primitive than Java, as we'll see in Chapter 5. A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version). To compile HelloWorld.java type the following at the terminal:
javac HelloWorld.java
If you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above.

Executing a Java program.

Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following at the terminal:
java HelloWorld

If all goes well, you should see the following response

Hello, World

Congratulations, you are well on your way to becoming a Java programmer.

Understanding the Java program.

The first 10 lines of HelloWorld.java are comments. They are not part of the program; they serve to remind us about its properties. The first two lines remind us what to type to compile and test the program. The next line describes the purpose of the program. The final lines give a sample execution of the program and the resulting output. We will always include such lines in our programs and encourage you to do the same. The key line with System.out.println prints "Hello, World" to the terminal. When we begin to write more complicated programs, we will discuss the meaning of "public," "class," "main," "String[] args", "System.out," and so on.

Creating your own Java program.

For the time being, all of our programs will be just like HelloWorld.java, except with a different sequence of statements in main. The easiest way to write such a program is to: As an example, try to create the program Hi.java using this method. This program uses a command line argument to get the program to type back out what we type in. After creating the program Hi.java, type in the following sequence of commands into the terminal to see how it works
javac Hi.java
java Hi Bob
Hi Bob. How are you?

java Hi Alice
Hi Alice. How are you?

Perspective.

Although the programs that we have written so far are quite simple, they embody the basic capabilities of everyday applications. Hi.java asks the user for input, and uses the information provided to produce some output. We can think of our program as a function that converts from an input string of characters (the command line argument) to an output string of characters (the message printed back to the terminal). This model is very simple, but sufficiently general to allow completion of any computational task. For example, the Java compiler itself is nothing more than a program that takes an input string of characters (a .java file) and outputs another string of characters (a .class file). Later, we will consider more sophisticated methods for program input and output, including graphical user interfaces. But, programs like Hi.java will enable us to solve many important computational problems without having to deal with more complicated input and output mechanisms.

Summary

Exercises

  1. Write a program TenHelloWorlds.java that prints "Hello, World" ten times.
  2. Describe what happens if, in HelloWorld.java, you omit
    1. public
    2. static
    3. void
    4. args
  3. Describe what happens if, in HelloWorld.java, you misspell (by, say, omitting the second letter)
    1. public
    2. static
    3. void
    4. args
  4. Describe what happens if you try to execute Hi.java with:
    1. java Hi
    2. java Hi @!&^%
    3. java Hi 1234
    4. java Hi.class Bob
    5. java Hi.java Bob
    6. java Hi Alice Bob
  5. Modify a Hi.java to make a program HiThree.java that takes three names and prints out a proper sentence with the names in the reverse of the order given, so that for example, "java HiThree Alice Bob Carol" gives "Hi Carol, Bob, and Alice.".

Web Exercises

  1. Write a program Initials.java that prints your initials using nine rows of asterisks like the one below.
    
    **        ***    **********      **             *             **
    **      ***      **        **     **           ***           **
    **    ***        **         **     **         ** **         **
    **  ***          **          **     **       **   **       **
    *****            **          **      **     **     **     **
    **  ***          **          **       **   **       **   **
    **    ***        **         **         ** **         ** **
    **      ***      **        **           ***           ***
    **        ***    **********              *             *
    

  2. Describe what happens if, in HelloWorld.java, you omit
    1. main
    2. String
    3. HelloWorld
    4. System.out
    5. println
  3. Describe what happens if, in HelloWorld.java, you omit
    1. the ;
    2. the first "
    3. the second "
    4. the first {
    5. the second {
    6. the first }
    7. the second }
  4. Describe what happens if, in HelloWorld.java, you misspell (by, say, omitting the second letter)
    1. main
    2. String
    3. HelloWorld
    4. System.out
    5. println