Back | Skip forward
hey, I'm itching to go! what was all that about?
"All that" was to make your programming environment as easy to use as
possible. In this section, however, we'll take a look at a sample
Java program and figure out how it works.
cool! so will this run on the web?
Nope. Basically there are two different kinds of Java programs: applets
and applications. Applets are what you see on the web, but trying to
program an applet before you know how to program an application is like
trying to learn to ride a motorbike before you know how to ride a bicycle
- difficult and often painful.
ow! this sounds harder than I thought.
It isn't really. Let's get down to business. Open EditPad using the
shortcut you created earlier. (You DID create a shortcut, didn't you?) Now
cut and paste the following code into EditPad.
//cut here-------
public class HelloWorld
{
String message;
public HelloWorld()
{
message = "Hello, World!";
}
public static void main(String args[])
{
HelloWorld hello1 = new HelloWorld();
System.out.println(hello1.message);
}
}
//cut here-------
Try to leave the indentations exactly as they are - they make the program
a little more readable.
I recommend making a folder within your desktop to store all this stuff. A
shortcut to the MS-Dos Prompt would be very helpful too.
Save the file as
HelloWorld.java
- make sure the H and the W, and no other letters, are in capitals
as Java is very picky about this sort of thing.
Now open up the MS-DOS Prompt or Command Prompt - this should be located
in your Start menu. Use the
cd
command to go to the folder in which you saved HelloWorld.java.
From the prompt, enter:
javac HelloWorld.java
and press Enter. This will invoke the Java compiler to convert the program
into Java bytecode, which can then be run.
If no error messages come crawling out from wherever they hide, you can
now run the program by entering:
java HelloWorld
Again, press Enter. If all goes well, you should see the following:
Hello, World!
So how does the beast work? Find out in the next
section.
Back | Forward