Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

Hello World

Java Programming, Lecture Notes # 10, Revised 10/01/99.

Preface
Introduction
The Java Version of Hello World
Interesting Code Fragments
Two C++ Versions of Hello World
Review Questions for Lesson 10

Preface

Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).

The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.
 

Introduction

Ever since the early days of C, programmers have been experimenting with new languages by programming the ubiquitous Hello World program. This lesson introduces you to Java programming by presenting and discussing some different versions of the Hello World program.

Two Approaches

Java programs can be written and executed in two ways:

Three Ways Actually

(Actually, it is also possible in many cases to write applets, which can be run in a stand-alone mode from the command line or can be run under control of a Java-capable browser. An example of such an applet will be presented in a subsequent lesson.)

Applets vs. Applications

Programming an "application" in Java is significantly different from programming an "applet." Applets are designed to be downloaded and executed on-line under control of a browser.

Restriction on Applets

Hence, their functionality is restricted in an attempt to prevent downloaded applets from damaging your computer or your data.

Applications Run Wide Open

No such restrictions apply to the functionality of a Java application. (This is not entirely true under Java Version 1.2, but the details are too complex for this introductory course.)

Class Definitions

All Java programs consist of one or more class definitions. In this course, we will refer to the primary class definition for a Java application as the controlling class.

The main() Method

A stand-alone Java application requires a method named main in its controlling class.

An Applet does not require a main method. The reason that a Java Applet does not require a main method will be explained in a subsequent lesson. 

Getting Started

HOW TO COMPILE AND RUN A JAVA APPLICATION

Here are the steps, based on the assumption that you are running under Win95.  If you are running under Solaris, Mac, or some other operating system, you will need to translate these instructions to that OS.

1.  Download and install the JDK from JavaSoft following the installation instructions at JavaSoft.  Be sure to also download and install the documentation that is a separate download package (as of 9/22/98).

2.  Using any editor that can produce a plain text file (such as Notepad), create a source code file with the extension on the file name being .java  This file contains your actual Java instructions.  You can copy some samples out of the early lessons in this tutorial to get started.

3.  Open an MSDOS box and change directory to the directory containing the source file.  It doesn't really matter which directory the source file is in, but I normally put my Java files in a directory all their own.

4.  Assume that the name of the file is joe.java, just to have something definitive to refer to.

5.  To compile the file, enter the following command at the prompt:

javac joe.java

6.  Correct any compiler errors that show up.  Once you have corrected all compiler errors, the javac program will execute and return immediately to the prompt with no output.  At that point, the directory should also contain a file named joe.class.  This is the compiled file.

7.  To run the program, enter the following command:

java joe


 

The Java Version of Hello World

The class File

Compiled Java programs are stored in "bytecode" form in a file with an extension of class where the name of the file is the same as the name of the controlling class (or other class) in the program.

main() Method is static

The main method in the controlling class of an application must be static, which results in main being a class method.

Class methods can be invoked without the requirement to instantiate an object of the class.

When a Java application is started, the Java interpreter finds and invokes the main method in the class whose name matches the name of the class file specified on the command line.

Running an Application

For example, to start the interpreter and run a Java application named hello1, a command-line statement such as the following must be executed at the operating system prompt:
 

java hello1

This statement instructs the operating system to start the java interpreter, and then instructs the interpreter to find and execute the java application stored in the file named hello1.class.

The Sample Program

This is a Java application named hello1.java.

When compiled, it produces a class file named hello1.class.

When it is run the interpreter invokes the main method defined in the controlling class.

The main method is a class method.

Class methods can be invoked without the requirement to instantiate an object of the class.

The program displays the following words on the screen:

Hello World

Interesting Code Fragments

The first fragment shows the first line of the class definition for the controlling class. I will discuss class definitions in detail later in the course. I also provide a brief discussion later in this lesson.

class hello1 { //define the controlling class

The next fragment begins the definition of the main() method. I will discuss method definitions in detail in a subsequent lesson.

  
public static void main(String[] args){

The next fragment causes the string Hello World to be displayed on the standard output device (the screen). This is an extremely powerful statement. When you understand how it works, you will be well on your way to understanding the Java version Object-Oriented Programming (OOP).

I will discuss this statement in more detail later in the course.

    System.out.println("Hello World");

The next fragment ends the main() method, and also ends the class definition for the class named hello1.

  }//end main
}//End hello1 class

The Complete Program Listing 

A complete listing of the program is shown below to put everything in context. 

/*File hello1.java Copyright 1997, R.G.Baldwin
This is a Java application program .

When compiled, this program produces the class named:

hello1.class

When the Java interpreter is invoked upon the application's
controlling class using the following statement at the
command line:

java hello1

the interpreter starts the program by invoking the main
method defined in the controlling class. The main method is
a class method which can be invoked without the requirement
to instantiate an object of the class.

The program displays the following words on the screen:

Hello World

*********************************************************/
class hello1 { //define the controlling class
  //define main method
  public static void main(String[] args){
    //display text string
    System.out.println("Hello World");
  }//end main
}//End hello1 class.

Comments

Java supports three styles of comments:

/** special documentation comment 
used by the javadoc tool */
/* Java/C/C++ style 
multi-line comment */
// Java/C/C++ style single-line comment

javadoc

The javadoc tool mentioned above is a program that is used to produce documentation for an application.

This type of documentation is very useful for on-line or on-screen documentation.

You should become familiar with the use of standard documentation for the JDK.

Overall Skeleton of Java Program

The overall skeleton of any Java program is a class definition.

All methods (functions) and variables must be defined inside a class definition. There can be no freestanding methods or global variables.

File Names and Extensions

The name of the controlling class should be the same as the name of the source file that contains it. Students in Prof. Baldwin's Introductory Java classes at ACC are required to organize their files that way.

Files containing source code in Java have an extension of java.

The main() Method

The controlling class definition for an application must contain the main method.

class Files

The file produced by the compiler containing the controlling class has the same name as the controlling class, and has an extension of class.

Many class Files May Be Produced

The java compiler produces a separate file for every class definition contained in an application or applet, even if two or more class definitions are contained in the same source file.

Thus, the compilation of a large application can produce many different class files.

What are Jar Files?

A capability known as a jar file can be used to consolidate those class files into a single file for more compact storage, distribution, and transmission.

main() is static

The controlling class for a Java application must contain a static method named main.

When you run the application using the Java interpreter, you specify the name of the class file that you want to run.

The interpreter then invokes the main method defined in the class file having that name. This is possible because a class method can be invoked without a requirement to instantiate an object of the class.

The main method defined in that class definition controls the flow of the program.

C++ Material

As mentioned earlier, I have also provided comparative C++ material in many of these lessons for the benefit of persons who already know C++ and are making the transition into Java. Java students in my classes are not required to learn the information that is specific to C++.

In an attempt to make it easy to identify the C++ material, I have attempted to color it green.
 

Two C++ Versions of Hello World 

The following C++ program also displays "Hello World" on the screen. This program doesn't use any classes and is therefore significantly different from a Java program. This program is followed by a different version of a C++ program that uses classes in a manner similar to the above Java program. 
 

/*File hello1.cpp Copyright 1997, R.G. Baldwin
This is a C++ program which produces the same output as the
Java program named hello1.java.

When compiled, this program produces the file named 
hello1.exe

When the exe file is executed, the program displays the 
following words on the screen:

Hello World
**********************************************************/

#include <iostream.h
void main()
{
  cout << "Hello World";
}//end main
//End C++ program

The following C++ program is structured similarly to the Java program shown above. This program contains a class that has a static member function that displays "Hello World." As in Java, static member functions in C++ can be invoked without the requirement to instantiate an object of the class. 

Whenever a stand-alone C++ program is executed, the main function is always the first function to be executed. In this program, a statement in the main function invokes the static member function of the class in much the same way that the Java interpreter invokes the static main method in the controlling class of a Java application. 
 

/*File hello2.cpp Copyright 1997, R.G.Baldwin
This is a C++ program which produces the same output as the
Java program named hello1.java and is structured to look 
more like the Java program than the previous C++ program 
named hello1.cpp.

This program defines a class named hello2 (similar to the 
class in hello1.java) which contains a static or "class" 
member function which displays a string.

Static member functions in C++ can be called without
instantiating an object of the class.

A statement in main() calls the static member function 
without instantiating an object.
*/
#include <iostream.h
class hello2 { //define the class named hello2
  public: //switch from private to public access
  //define a static member function which will display 
  // a string
  static void classMain() {cout << "Hello World";}
};//End class hello2.
void main(){
  //Call the static member function without an object to
  // display the string.
  hello2::classMain();}//end main
//End C++ program

 

Review Questions for Lesson 10

Q - Applications are designed to be downloaded and executed on-line under control of a web browser, while applets are designed to be executed in a stand-alone mode from the command line: True or False?

A - False. Applications are for stand-alone use while applets are for browsers.

Q - All applications and applets written in Java require a main method: True or False?

A - False. Applets do not require a main method while applications do require a main method.

Q - Explain the relationship between the name of the class file for a Java application and the location of the main method in the application.

A - The name of the class file must be the same as the name of the class which contains the main method (sometimes called the controlling class).

Q - Explain how you cause a method to be a class method in Java.

A - Preface the name of the method with the static keyword.

Q - Class methods can be invoked without the requirement to instantiate an object of the class: True or False?

A - True

Q - Write the source code for a Java application that will display your name and address on the standard output device. Show the command-line statement that would be required to execute a compiled version of your application.

A -

/*File Name01.java
This is a Java application that will display a 
name on the standard output device.

The command required at the command line to execute this
program is:

java Name01

*/

class Name01 { //define the controlling class
  public static void main(String[] args){ //define main
    System.out.println(
     "Dick Baldwin\nAustin Community College\nAustin, TX");
  }//end main
}//End Name01 class.

Q - Show the three styles of comment indicators that are supported by Java.

A -

/** special documentation comment used by the JDK javadoc tool */
/* C/C++ style multi-line comment */
// C/C++ style single-line comment

Q - Does Java allow free-standing functions or methods?

A - No, Java does not allow free-standing functions or methods.

Q - What is the relationship between the name of the controlling class in an application and the names of the files that comprise that application.

A - One of the files must have the same name as the controlling class with an extension of class.

Q - What is the relationship between the number of classes in an application and the number of separate files with the class extension that go to make up that application? How does this change when all the classes are defined in the same source file?

A - Each class definition results in a separate class file regardless of whether or not the classes are defined in separate source files.

Q - Class methods in Java can only be invoked relative to a specific object: True or False?

A - False. Class methods can be invoked by joining the name of the class with the name of the method using a period.

Q - Write the signature line for the main method in a Java application.

A - public static void main(String[] args)

Q - Write a Java application that meets the following specifications.
 

/*File SampProg02.java from lesson 10
Copyright 1997, R.G.Baldwin
Without reviewing the following solution, write an 
application that will display your name on the screen.
**********************************************************/
class SampProg02 { //define the controlling class
  public static void main(String[] args){ //define main
    System.out.println("Dick Baldwin");
  }//end main
}//End SampProg02 class.

-end-