Java Reference
In-Depth Information
Step 1: Write the Source Code
The first step is writing the code for our Java program. We will write the Java
programs in this topic using a simple text editor such as Windows Notepad.
(You Unix folks can use emacs or vi.) The Java code is initially text, and the
javac tool will compile our text files into bytecode.
Figure 1.6 shows you the Hello, World program typed into Notepad. Open
your text editor and then type in the program just as you see it in Figure 1.6.
Keep in mind that Java is case sensitive, meaning, for example, that String and
string are not the same in Java.
Let's save this file first and then discuss what the program does. Java is a
highly structured and organized language. One of the rules that must be fol-
lowed is that the name of a source code file must match the name of the public
class defined in that file, and the file extension must be .java.
Create a new directory off your root directory (off your c:\ drive in Win-
dows) named javafiles. Save the HelloWorld.java file in that javafiles directory.
This way you can access it quickly from the command prompt. The name of
the public class in Figure 1.6 is HelloWorld, so this file must be saved as Hel-
loWorld.java.
Some programs such as Notepad use a default extension like .txt for text
files. Because this won't work in Java, make sure that you save the source file
correctly. One way to ensure a correct filename is to enclose the filename in
quotation marks.
Do not save your Java source files in a directory with spaces in the name
of the directory. This may cause problems, depending on the version of
Windows you are using; therefore, do not save your files in the My
Documents folder.
Figure 1.6
The source code of the Hello, World program.
Search WWH ::




Custom Search