Section 2.5. Anatomy of a Simple Java Program

   

2.5 Anatomy of a Simple Java Program

Let's write the simplest possible Java program to begin. When run, this program will output the text string "Hello," along with the value of the name variable to the command line.

2.5.1 Hello.java

 /* file: Hello.java     purpose: print a simple string to the command line    author: E Hewitt    created date: 4.12.02 */ package javaforcf; // declare the name and visibility of this class public class Hello { // main method accepts an array of strings as arguments public static void main (String [] args) { // create a new String object with the value of 'Jeremy' String name = new String("Jeremy"); // print the static text and the variable together // out to the command line System.out.println("Hello," + name);         } } 

To run this program:

  1. Type the code into a plain text editor, and save it as JAVA_HOME\Java-ForCF\Hello.java (not hello.java , or anything elsethe class name must match the file name).

  2. Open a command window and cd to JAVA_HOME\JavaForCF .

  3. Type javac Hello.java .

  4. If the program compiles correctly, a new prompt will appear. There won't be any message from the compiler. A new file will appear in that directory. If there are errors in the code, the compiler will notify you with a line number, the nature of the error, and the total number of errors found. If you get an error saying "error:cannot read: Hello.java," that means that the compiler can't find the program to compile it. Make sure that you have navigated to the same directory in which the . java file exists. You can list the contents of a directory at a Windows command prompt by typing dir .

  5. Once the program has compiled without error, you can run it. To do this, type: java Hello . Remember that the Java programming language is case sensitive; typing java hello won't work.

The program will output

 Hello, Jeremy 

Now let's take a moment to look at the code. For now, we will gloss over some of the concepts and look at them in detail in the upcoming chapters. The first block is an SQL-style comment.

Note

ColdFusion developers will notice that comment syntax is the same in Java as in CFScript or in SQL. That is, use a /* to start a multi-line comment and / / for a single-line comment. You have likely also noticed that semi-colons in Java are used in the same way they are in CFScript. You must end statements with semicolons.


The first significant line of the program is the class declaration: public class Hello { . All Java programs need to define a class that will serve as a repository for data and define the behavior that they are capable of (their methods ). Your source file can contain only one public class statement. Don't worry about the public keyword just yet. It basically means that this class is publicly accessible; that is, any class can use it.

Note

Curly braces in Java are used much as you use them in CFScript. In Java, you define opening and closing boundaries of class definitions, methods, and code blocks with curly braces.


The name of the class must correspond exactly to the name of your resulting . java file. Java is a case-sensitive language. Hello.java is not the same as hello.java . By the same token, String = Name; and String = name; create two different variables .

Note

Unlike ColdFusion, Java is ruthlessly case sensitive. Spaces, however, can be used rather freely . For instance, main (String [ ] args) is read by the compiler just as main(String[] args) is. You can indent away all you like, willy-nilly. This behavior is the same as in ColdFusion.


2.5.2 The main Method

Inside the class code block, you declare your variables and your methods. Java features a special method called main , which serves as the core structure for your program's execution. The Java Virtual Machine starts processing any Java program with main . So everything that will happen in this program is written inside main .

For the first few chapters in the book we will write largely procedural programs. These programs will have their code inside the main method of a single class file. A procedural program runs the way a Web page is interpretedonce over, from top to bottom. The advantage to doing this while learning is that it is much easier to analyze concepts as you go. Because Web page processing is familiar and straightforward, like regular reading, it helps you learn faster. Second, there is a good deal to figure out about variables, methods, the API, visibility, and so on, before you can really start using objects in any meaningful way. However, there is a disadvantage to this approach as well, which is that you must make a transition in thinking once we start doing real object-oriented programming. That is to say, the way we will write small programs as we get started is not how larger, object-oriented Java applications are written. But we will cross that bridge when we need to.

The main method accepts as arguments an array of strings, which we will call args. main must always be declared as accepting an array of String objects, which you can pass from the command line when you run the program. However, the name of this array is arbitrary. We will call it args , since that is the convention.

The first thing we do in the program is create an object of type String . Almost everything in Java is an object (primitive data types, such as boolean s and int s, are not objects). Creating an object is often referred to as instantiating an object, because you are creating a concrete instance of what is otherwise just a definition (the object's class). For instance, Jeremy Allaire is a particular instance of the CTO class. Once you have a String object, you need to set its value to something (when a String object is created, its value is an empty Stringit is not null).

 String name = new String("Jeremy"); 

There are other ways to set a value for a String object; this is the most explicit. What this line says is: Hold a place in memory for a variable of type String . I'm going to refer to this string as name . Create the String object with a value of Jeremy .

Next, we call the System.out.println( ) method to print out the value of the string to the command line. The main method ends, as does the class definition, and program execution stops.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

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