A Very Simple Java Program


So far we have seen declaration and assignment lines, but only as code fragments. If you type any of the fragments into a file and try to compile the file, you will get nothing more than compiler error messages. This is because a well-formed Java program—even one that does almost nothing—must conform to certain structural rules.

And here we have a problem, which is best illustrated by an example. The following code listing is a complete Java program that contains a declaration and an assignment.

public class VerySimple {   public static void main(String[] args)   {     double age;     age = 123.456;   } }

The problem is this: the program contains a number of words and symbols that have not yet been introduced, and that will require considerable explanation when the time comes. So for now, you just have to accept the mysterious parts of this code as things that must be done to make the program work.

Type the program into a file called VerySimple.java. Compile it by typing javac VerySimple .java. If you get compiler error messages, make sure you've typed in the program exactly as it appears here. The compiler output will be a file called VerySimple.class.

The preceding program appears just as it would in a source file. However, when listings of more than a few lines appear in print, it is convenient to number the lines:

1. public class VerySimple 2. { 3.   public static void main(String[] args) 4.   { 5.     double age; 6.     age = 12.34; 7.   } 8. }

The line numbers are convenient for referring to features of the code, but they should never appear in source code that is to be compiled. Here, the line numbers let us point out that the relevant parts of the listing are lines 5 and 6, and all the rest is mysterious code that will be explained later.

Output

The SimCom virtual machine lets you see all of memory all the time, but Java's memory is hidden. In the VerySimple program, there is no way to see the value of age. The following program declares and assigns age, and then prints its value to the console:

1. public class VerySimple2 2. { 3.   public static void main(String[] args) 4.   { 5.     double age; 6.     age = 12.34; 7.     System.out.println(age); 8.   } 9. }

The new line is #7. To print out any value, you can use the statement System.out .println(theValue);.

Here again, we ask you to accept that the syntax works. The explanation of why it works will come as soon as we have covered all the underlying concepts. For now, be aware that in order to print the value of any variable, you need to type that variable's name between the parentheses in a line like #7.

Notice that in line #1, the word following class has been changed from VerySimple to VerySimple2. The name following class has to match the name of the source file. Therefore, if you want to type in this program, you should store it in a file called VerySimple2.java. The compiler will generate an output file with the same name, followed by the .class suffix: VerySimple2.class. This compiler-output file is known as a class file. To run the application, type java VerySimple2. Table 2.3 summarizes this naming consistency.

Table 2.3: Naming Consistency

Name in class line

Source filename

Class filename

Invocation

VerySimple2

VerySimple2.java

VerySimple2.class

java VerySimple2

Printing out the value of a variable is convenient, but it would be even more convenient to print out a reminder of what the value represents. "Age is 12.34" is much more informative than "12.34." The following program prints out the more informative line:

1. public class VerySimple3 2. { 3.   public static void main(String[] args) 4.   { 5.     double age; 6.     age = 123.456; 7.     System.out.println("Age is " + age); 8.   } 9. }

In line #7, the text inside the double quotes is known as a literal string. The plus sign does not indicate addition, since adding text to a number doesn't really mean anything. In this context, the plus sign just means that the literal string is to be printed out, followed by the value of age. Within the parentheses of a println statement, you can have any number of alternating literal strings and variables. So if you wanted to print out the values of variables i, j, and k, separated by commas, you could use the following line:

System.out.println(i + "," + j + "," + k);

Now that you can declare, assign, and display variables, you are ready for the next step: mathematical operations. That is the topic of the next chapter.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

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