Java Reference
In-Depth Information
before learning about the different IDEs available. You learned in Chapter 1 about source code and
byte code. When you think of programming or coding, you are thinking about writing source code.
Since you're learning basic programming in Java, you must type statements using the specific lan-
guage and structure that Java prescribes. Source code doesn't look exactly like writing you'd read in
a newspaper, but it's relatively readable to humans, particularly those who have learned Java basics.
It's essentially text, and for that reason, you can write it in a text editor like Notepad or any similar
program.
After you've written the source code, a Java compiler can translate what you've written into
machine‐readable byte code. That's one of the features that your newly installed JDK offers. Once
your code has been compiled, you can run the program. In the first exercise, you'll go through these
three steps to create, compile, and run your first Java application.
Creating Your First Java application in Notepad
try it out
In this exercise, you code a very simple Java program in a text editor, then compile and run it from the
command window of your computer.
1.
Open Notepad or a similar text editor and start a new text file. On Windows 7 and earlier, you can
open Notepad by opening the Start menu. Then under All Programs, find the Accessories folder.
In Windows 8, you can access the Apps screen by clicking the down arrow at the bottom of your
screen.
2.
Enter the following code in your text file:
/**
* This is a simple Java application.
* It prints a short statement to the standard output.
*/
class MyFirstApplication {
public static void main(String[] args){
System.out.println("I've coded, compiled, and run my first Java program!");
}
}
Be aware that Java code is case‐sensitive, so pay attention to upper‐ and lowercase letters as you
type. MyFirstApplication , myfirstapplication , MyFIRSTApplication , and so on are all dif-
ferent names.
3.
Save the file as MyFirstApplication.java (as before, watch for capitalization and be consistent
between the name of your class and the filename). Save your file as a text document with the *.txt
extension. If you do not specify the encoding, the platform default converter is used. For this exer-
cise, save the file in a location that you can easily find. You should end up with something like
Figure 3-1 when you go to save.
4.
Close Notepad and open a command window. In Windows 7 and earlier, you can click Start
Run, then type cmd and press Enter. On a Windows 8 machine, you can go back to the Apps screen
and scroll to the Windows System section where you will find the command prompt.
Search WWH ::




Custom Search