A Simple Java Application Program With Full Descriptions

0




A Simple Java Application

Like any other programming language, you use the Java programming language to create applications. Code 1-1 and Code 1-2 show a simple Java application that prints a greeting to the world.

Code 1-1 The TestGreeting.java Application

1 //

2 // Sample “Hello World” application

3 //

4 public class TestGreeting {

5 public static void main (String[] args) {

6 Greeting hello = new Greeting();

7 hello.greet();

8 }

9 }

Code 1-2 The Greeting.java Class

1 public class Greeting {

2 public void greet() {

3 System.out.println(“hi”);

4 }

5 }
The TestGreeting Application

Code 1-3 Lines 1–3

1 //

2 // Sample “Hello World” application

3 //

Lines 1–3 in the program are comment lines.

Code 1-4 Line 4

4 public class TestGreeting {

Line 4 declares the class name as TestGreeting. A class name specified in a source file creates a classname.class file when the source file is being compiled. If you do not specify a target directory for the compiler to use, this class file is in the same directory as the source code. In this case, the compiler creates a file called TestGreeting.class. It contains the compiled code for the public class TestGreeting.

Code 1-5 Line 5

5 public static void main (String args[]) {

Line 5 is where the program starts to execute. The Java technology interpreter must find this defined exactly as given or it refuses to run the program. Other programming languages, notably C and C++, also use the main() declaration as the starting point for execution. The various parts of this declaration are briefly described here. The details are covered later in this course.

If the program is given any arguments on its command line, these are passed into the main() method in an array of String called args. In this example, no arguments are used.

The following describes each element of Line 5:

● public – The method main() can be accessed by anything, including the Java technology interpreter.

● static – This keyword tells the compiler that the main() method is usable in the context of the class TestGreeting. No instance of the class is needed to execute static methods.

● void – This keyword indicates that the method main() does not return any value. This is important because the Java programming language performs careful type-checking to confirm that the methods called return the types with which they were declared.

● String args[] – This method declares the single parameter to the main method, args, and has the type of a String array. When this method is called, the args parameter contains the arguments typed on the command line following the class name; for example:

java TestGreeting args[0] args[1] . . .

Code 1-6 Line 6

6 Greeting hello = new Greeting();

Line 6 illustrates how to create an object, referred to by the hello variable. The new Greeting syntax tells the Java technology interpreter to construct a new object of the class Greeting.

Code 1-7 Line 7

7 hello.greet();

Lines 7 demonstrates an object method call. This call tells the hello object to greet the world. The implementation of this method is shown on Lines 3–5 of the Greeting.java file.

Code 1-8 Lines 8–9

8 }

9 }

Lines 8–9 of the program, the two braces, close the method main() and the class TestGreeting, respectively.

Code 1-9 Line 1

1 public class Greeting {

Line 1 declares the Greeting class.

Code 1-10 Lines 2–4

2 public void greet() {

3 System.out.println(“hi”);

4 }

Lines 2–4 demonstrate the declaration of a method. This method is declared public, making it accessible to the TestGreeting program. It does not return a value, so void is used as the return type.

The greet method sends a string message to the standard output stream. The println() method is used to write this message to the standard output stream.

Code 1-11 Line 5

5 }

Line 5 closes the class declaration for Greeting.

Code 1-9 Line 1

1 public class Greeting {

Line 1 declares the Greeting class.

Code 1-10 Lines 2–4

2 public void greet() {

3 System.out.println(“hi”);

4 }

Lines 2–4 demonstrate the declaration of a method. This method is declared public, making it accessible to the TestGreeting program. It does not return a value, so void is used as the return type.

The greet method sends a string message to the standard output stream. The println() method is used to write this message to the standard output stream.

Code 1-11 Line 5

5 }

Line 5 closes the class declaration for Greeting.