CS 3230 - Lab 1

"Hello World" in Java

Every computer science class is required by law to teach one of three things: (1) the OSI 7-layer model, (2) the "Towers of Hanoi" problem, or (3) a "Hello World" program. You lucky guys get to do "Hello World". This page gives you a step-by-step on how to do it. I will go over this on Thursday in the lab and be available for questions if you have any.

Step-by-Step

Step #1 - Text Editor

Start the Vim editor (Start -> Programs -> Vim -> gVim easy). You will likely want to use the "easy" selection unless you're a die-hard Unix guy. There may even be a shortcut on the desktop.

Note: You do not have to use Vim if you don't want to; all you need is a text editor of some sort. Use Notepad or Visual Studio for all I care. I strongly suspect, however, that once you see all your classmates using Vim, you'll want to use it too.

Step #2 - Program Code

Type in the following code:

// Your Name
// Lab 1 - Hello World
// CS 3230 - The Java Class

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

The commented lines at the top are very important; I want to see them at the top of every assignment you turn in. If you don't put those lines on there, I won't (can't!) give you credit.

Step #3 - Save File

Save the text you have just typed in to a file called "HelloWorld.java".

Important Note: The filename you choose must be the same name as the class with the main method. If you save it to a different name, the compiler will yell at you and not let you run the program. I will probably have a test question about this. Seriously.

Another Important Note: Other students use the same lab computers you do. There is no guarantee that they will treat your work with the same care and consideration that you do. If you don't want to lose your work, save it to some kind of secondary storage (floppy, thumb drive, etc.).

Step #4 - DOS prompt

Start a DOS prompt by clicking 'Start -> Run' and typing cmd in the little "Run" box. You now need to change into the same directory where you saved your program (CD C:\WHEREVER\YOU\SAVED\IT).

Step #5 - Compile Program

At this point you should be able to compile your program by typing javac HelloWorld.java. ("javac" is the name of the Java compiler -- Looks like another test question to me!) If it reports errors, you need to fix them. If all goes well, it will report nothing but generate a "HelloWorld.class" file. (Type dir to see it.)

Note: If you get the message "Program not recongnized" (or similar) it's because the JDK programs are not in your path. You'll need to fix this, for now, by typing: PATH=%PATH%C:\JDK1.4.1 (or whatever the path happens to be). I'll try to make sure this is all fixed and ready, though.

Step #6 - Run Program

Now you can type java HelloWorld to run your program. Note that you do not use a .java or .class extension after HelloWorld, just the name of the class. Incidentally, "java" is the name of the virtual machine that your bytecode runs in. (Test questions galore!)

Step #7 - Turn It In

After you get it all done, show me your running code. Then, you can print it up and turn it in. Per Step #2, make sure your name, lab # and course # are at the top.

Note: This assignment is not actually due until the class after the Midterm, but if you want to turn it in early, that'd be just ducky.

Explanation of The Code

Your book explains what is going on with this code starting at the bottom of pg.35 ("A Simple Java Program") and on through to the top of pg.39 (as soon as you hit the section "Data Types"). The following list gives a brief synopsis. The alert student will note many similarities to C++.

comments: Lines beginning with '//' are commented til the end of the line. Multi-line comments can be made with /* and */ (but they do not nest).

Case-Sensetive: The identifier 'Box' is different from the identifier 'box'. (The bottom of p.42 gives a little side note about this.)

public keyword: Access modifier; more on this later.

class keyword: All java code must live inside a class, even the main() method.

HelloWorld: Class name, must match filename (+.java extension)

curly braces: The curly braces ({ and }) are used to delimit a block -- a discrete section of code in your program. Most namespace / control flow keywords are followed by a block. When curly braces follow a class header or method header, it is usually referred to as the class body or method body.

static: Class-method rather than instance method. If you don't understand this, ignore it for now.

void: Method does not return a value.

main: Method where everything begin. Most stand-alone Java programs have a main method somewhere. Most of yours will.

parentheses after main: holds a list of arguments passed to the method.

String[] args: String array containing arguments passed to the program. The String class is similar to the ANSI C++ string class. More on that in chapter 3.

System.out.println: This is how you send a line of output to the console, followed by a newline (carriage return). It is analagous to printf in C or cout in C++. Get used to typing this. There is also a print method that does not print the trailing newline.

parentheses after println: Holds a list of arguments we are passing to println.

"Hello World!": A string literal, delimited by double-quotes.

semicolon: Every statement must end with a semicolon; carriage returns and all other whitespace are meaningless. That having been said, you should use whitespace to make your code more readable and split long statements across multiple lines.

Questions and Answers

How come we're not using [insert favorite Java IDE]?

Many of you are no doubt wondering why we are editing and compiling in such a "primitive" fashion. After all, there are oodles of whiz-bang, gee-whiz, nifty/futzy graphical IDEs out there, why not use one of them? Hunh? Hunh? Why? Hunh?

Answer: Trained monkeys can use those IDEs. I want to teach you guys how to be actual programmers by focusing on your code rather than how many widgets you can fit into a 1280x1024 screen.

Another Answer: If you can learn how to edit, compile, and run Java programs using the "primitive" command-line, I'm sure you'll be able to do it with an IDE. The reverse is not also true, though; if the only thing you've ever used is a GUI IDE, you'll be lost without it.

That having all been said, if you really want to use an IDE, I'm not going to stop you. The thing I care most about is seeing your code run.