Identifiers and Identifier Naming Rules


Creating Simple Java Programs (Applications)

To create a Java program you must define a class that contains a main() method, compile the source file with the javac command-line tool, and then run the resulting .class file using the java command-line tool. Doing so requires you to have an understanding of several skills, namely, you must:

  • understand how to create a source file using a text editor

  • understand the basic structure of a Java class definition

  • understand the structure of a Java application and the purpose of the main() method

  • understand how to name the source file with the same name as the public class it contains

  • understand how to compile the file with the javac compiler

  • understand how to run the resulting class file with the Java virtual machine

If you encounter any difficulty along the way you will also need the qualities of tenacity and patience. You cannot be someone who gives up easily or grows impatient when things go a little wrong. The programming profession rewards those who refuse to accept defeat!

Structure Of A Java Application

A java application, in its simplest form, consists of a class definition that contains a main() method. Example 6.1 illustrates these points.

Example 6.1: SimpleApplication.java

image from book
 image from book 
image from book

Although SimpleApplication is in fact a simple application, there’s a lot going on here that’s worth noting. First, the definition of SimpleClass begins on line 1 with the use of the Java keywords public and class. The meaning of each of these keywords is discussed in greater detail in the next section. For the purposes of this example these keywords are required to tell the Java compiler that we are declaring a new data type (class) that will be publicly accessible, meaning other classes or objects can use this class if they so require.

These keywords are followed by a string of characters that form the name of the class. The string of characters “SimpleApplication” is formally referred to as an identifier. Identifiers are used to form the name of classes, variables, constants, and methods. There are a few rules you must learn regarding the formation of identifier names and theses rules will be covered later in this chapter.

The opening brace appearing at the end of line 1 marks the beginning of the body of the class definition. Everything appearing between the opening brace at the end of line 1 to the closing brace on line 3 belongs to the SimpleApplication class.

The entire main() method definition begins and ends on line 2. The definition of the main() method begins with the keywords public, static, and void. The public keyword means that main() can be called from other objects. The keyword static means the main() method is a class-wide method that can access static class fields. You will learn more about static and non-static methods in chapter 9. The keyword void indicates the main() method does not return a result when it completes execution.

These keywords are followed by the name of the method — main. The method name is followed by a set of parenthesis that can optionally contain one or more method parameters. Method parameters are used to pass data to a method when the method is called. In the case of the main() method it is required to take a parameter of type String array. (String[]) The parameter name args is an ordinary identifier that could be any name. For example, you could declare your main() method to look like this:

 public static void main(String[] arguments){}

Notice the only thing different here is the name of the method parameter. Everything else stayed the same. For the sake of completeness, one more change could be made to the main() method declaration:

 public static void main(String arguments[]){}

The change here is subtle but important. Java allows you to declare arrays using C++ syntax. Notice that the brackets were moved to the right side of the parameter name. The purpose and use of the main() method’s String array is discussed in detail in chapter 8.

Returning to example 6.1, the start of the main() method body is indicated by the opening brace and its end is denoted by the closing brace. In this example the main() method body is empty. There is nothing in the body of the main() method and therefore when SimpleApplication is executed there will be no visible results.

As you can see, although SimpleApplication does little useful work you can learn a lot from its close examination.

Compiling and Executing SimpleApplication

Compiling and executing the SimpleApplication class is straightforward assuming you have properly set up your Java development environment. (See chapter 2.)

To compile the SimpleApplication source file make sure you have saved the file with the name image from book SimpleApplication.java. Use the javac compiler tool to compile the source file as is shown here:

 javac SimpleApplication.java

Notice that you must enter the complete file name, including the .java extension, when you compile the source file. This will yield a new file named image from book SimpleApplication.class. This class file contains the byte codes that will be loaded into and executed by the Java virtual machine. To execute the image from book SimpleApplication.class file use the java tool as is shown here:

 java SimpleApplication

Note that in this case the use of the complete filename was unnecessary. The .class filename extension is assumed.

When you run SimpleApplication you will see nothing happen. That’s because the main() method does nothing except start and stop. Let’s expand the capability of SimpleApplication by adding a line to the main() method that will print a short text message to the console. Example 6.2 gives the source code for the modified SimpleApplication class.

Example 6.2: SimpleApplication.java (version 2)

image from book
 1     public class SimpleApplication{ 2        public static void main(String arguments[]){ 3          System.out.println("SimpleApplication lives!"); 4       } 5      }
image from book

Figure 6-1 shows the process of compiling image from book SimpleApplication.java and the results of executing the image from book SimpleApplication.class file.

image from book
Figure 6-1: Compiling and Executing SimpleApplication

The SimpleApplication class now does some useful work. It uses the services of another Java platform API class named System. The System class provides console I/O capability ready for you to use in your programs. Here I have used the services of the System’s out object and the out object’s println() method to print a short text string to the console.

The important thing to take away from this example is that SimpleApplication now enlists the help of another Java class. The System class is but a tiny fraction of the pre-built functionality supplied by the Java platform classes.

Quick Review

A Java application is created by adding a main() method to an ordinary class definition. The keywords public and class are used in the class definition. The name of the class is formulated according to the Java identifier naming rules. The body of the class definition appears between the opening and closing braces. Everything in the class body belongs to the class.

The main() method appears in the class body. The name of the main() method is proceeded by the keywords public, static, and void. The main() method takes a String array as its only parameter. The body of the main() method is contained between opening and closing braces.

Building Bigger Applications

Now that you know the basic structure of a Java application it’s time to build upon this knowledge so that you can write applications that do more than just print simple text messages to the console. To do this you must learn more about the fundamental building blocks the Java programming language provides to help in this endeavor. The building blocks include reserved keywords, primitive, reference, and array data types, statements, expressions, operators, variables, and constants, and how to formulate variable and constant names using identifier naming rules. The rest of this chapter is devoted to covering this material. Don’t feel as though you have to learn everything discussed below by heart. Some of the material is intended for future reference.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net