This worksheet will lead you along the first steps in learning Java. Please work through it all. The things you need to develop and hand in are listed at the end.
· Create a Java source file. A source file contains text, written in the Java programming language. You can use any text editor to create and edit source files.
· Compile the source file into a bytecode file. The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a bytecode file.
· Run the program contained in the bytecode file. The Java VM is implemented by a Java interpreter, java. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.
a. Get a Java Source File.
Download the file HelloWorldApp.java to your current directory.b. Compile the Source File.
At the prompt, type the following command and press Return:
javac HelloWorldApp.java
The compiler generates a Java bytecode file, HelloWorldApp.class. At the prompt, type ls to see the new file that was generated.
Now that you have a .class file, you can run your program.c. Run the Program.
In the same directory, enter at the prompt:
java HelloWorldApp
Now you should see:
Hello World!
Now that you've seen a Java application (and perhaps even compiled and run it), you might be wondering how it works and how similar it is to other Java applications. Note that a Java application is a standalone Java program-- a program written in the Java language that runs independently of any browser.
This section dissects the "Hello World" application you've already seen. Here, again, is its code:
/* * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ Class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); //Display the string. } }
The "Hello World" application has two blocks of comments. The first block, at the top of the program, uses /* and */ delimiters. Later, a line of code is explained with a comment that's marked by // characters.
The comments work like this:
/* text */
The compiler ignores everything from /* to */.
// text
The compiler ignores everything from // to the end of the line.
The first bold line in the following listing begins a class definition block.
/* * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ Class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); //Display the string. } }In the Java language, each method (function) and variable exists within a class or an object (an instance of a class). Thus, the skeleton of any Java program is a class definition.
A class--the basic building block of an object-oriented language such as Java--is a template that describes the data and behavior associated with instances of that class. When you instantiate a class you create an object that looks and feels like other instances of the same class. The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods. Methods are similar to the functions or procedures in procedural languages such as Fortran.
Julia Child's recipe for rack of lamb is a real-world example of a class. Her rendition of the rack of lamb is one instance of the recipe, and mine is quite another. (While both racks of lamb may "look and feel" the same, I imagine that they "smell and taste" different.)
A more traditional example from the world of programming is a class that represents a rectangle. The class would contain variables for the origin of the rectangle, its width, and its height. The class might also contain a method that calculates the area of the rectangle. An instance of the rectangle class would contain the information for a specific rectangle, such as the dimensions of the floor of your office, or the dimensions of this page.
In the Java language, the simplest form of a class definition is
class name { . . . }The keyword class begins the class definition for a class named name. The variables and methods of the class are embraced by the curly brackets that begin and end the class definition block. The "Hello World" application has no variables and has a single method named main.
The first bold line in the following listing begins the definition of a main method.
/* * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ Class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); //Display the string. } }Every Java application must contain a main method whose signature looks like this:
public static void main(String[] args)
The method signature for the main method contains three modifiers:
· public indicates that the main method can be called by any object.
· static indicates that the main method is a class method. This means that the method is associated with the class and not with an instance of the class.
· void indicates that the main method doesn't return any value.The entry point of every Java application is its main method. When you run an application with the Java interpreter, you specify the name of the class that you want to run. The interpreter invokes the main method defined within that class. The main method controls the flow of the program, allocates whatever resources are needed, and runs any other methods that provide the functionality for the application.
The other components of a Java application are the supporting objects, classes, methods, and Java language statements that you write to implement the application. We will be talking more about these issues in a little while.
Now it is your turn.
Useful information