In this lesson, you will learn
What is so unique about Java that has propelled its rapid, wide acceptance?
The stuff you will enter (text) has a very specific structure (its syntax) that the Java compiler expects. You create your Java programming language files with an editor that is available on your computer. On a PC running Windows, Wordpad or Notepad will work just fine. On a Sun workstation, textedit is a nice editor.
Once you have some complete Java source code in a file, you compile it. The Java compiler turns your file full of characters into another file which contains instructions that a JVM (Java Virtual Machine) can interpret, a ".class" file.
|
| |
The Java Virtual Machine takes over from here. JVMs exist for any computer and operating system, for example PCs running Windows, Sun Microsystems' computers running Solaris or Linux, Palm Pilots and Handspring Visors running PalmOS, etc... The JVM takes your ".class" file, loads it into its virtual memory, links lots of stuff together, and then starts interpreting/executing the program. During linking, your class file will be combined with other classes that are part of the Java environment. Standard classes exist for helping you do things like displaying text on the display, get characters typed on the keyboard, display graphics stuff, communicate over the Internet,... And, then, off it goes; your program comes to life.
|
| |
To get going you need to start by working your way through what's known as the "Edit, Compile, Execute" process or cycle. This is something you will tend to do over and over as you work on your Java programs. This exercise will give you a feel for how you will be working in the Java environment. To make this quick and simple, you'll start with what is the shortest Java program possible.
Type the following text into your computer, putting it in a file called "Hello.java"
class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
} // end class Hello
|
| Example 16.1 |
Once you have it in a file in the computer, check it to see that it matches the example - character for character, upper-case only where the example shows upper-case characters, the same punctuation, etc... When you think you have it, make sure to save the text to the file: Hello.java.
Now it's time to run the Java compiler with the file "Hello.java" as input. This will get you your "Hello.class" file. Type in:
javac Hello.java
Did it complain? Did it find a typo that you missed? If so,
go back into the editor and compare your text with Example 16.1 again.
The compiler tells you the line number that it detected an error
on. This should help. But, the error can be on an earlier
line too. Repeat this "Edit, Compile" cycle until you get the
compiler to quit complaining.
Now you can use the JVM to execute your program. Type in:
java Hello
Notice that you don't type "java Hello.class" even though you had to
provide the full filename to the Java compiler. The Java Virtual
Machine (JVM) assumes and looks for the file Hello.class even though
you only provided the class name Hello, not the filename. If you
have everything right, the system should respond with:
Hello World!
Cool... You've just entered and executed your first Java application.
You've used the println command in your TurtleTalk programs too. I added this command in preparation for your move to Java. In standard Logo, the print command does what TurtleTalk println does. But, in Java, print does not add the newline character to the output - just like the way print works in TurtleTalk. In Logo, the type command leaves off the newline.
| TurtleTalk Command |
Logo Command |
Java Method |
Description |
| type | Display the text provided as its input/argument. | ||
| println | println | Display the text provided as its input/argument, followed by a newline character. | |
| Table 16.1 | |||
Play around, try both methods.
A class' name, must match the name of the file that it is in. The Java source code will compile without any indication of problems. But, when you try to execute it, the JVM will complain that it can't find your class, the name of which is the file name.
Finally, notice that the java compiler command, javac, expects a full filename including the ".java" part; but, the java command will not accept the full filename - it expects to find a file with the name you specify, ending in ".class"