The HelloWorld Program

The HelloWorld Program

java the hello world program
When learning a new programming language, the first task is normally to write a program which simply prints the words “Hello World” to your computer’s display. Once we have written, compiled, and executed the program, and verified that “Hello World” appears on the display, we have successfully run the HelloWorld program.

Section 1 - Creating and Running the HelloWorld Program 

In your favorite text editor, or in your programming Integrated Development Environment (IDE) , create a file named HelloWorld.java which contains the following:

Listing 1 - HelloWorld.java

1 public class HelloWorld 
2 {
3 public static void main(String[] args) 
4 { 
5 System.out.println("Hello World"); 
6 } 
7 }

To compile and run HelloWorld.java from the command line, type the following:
javac HelloWorld.java 
java HelloWorld 

The Javac command runs the Java compiler which converts Java source file HelloWorld.java into bytecode file HelloWorld.class . Java source files are text files containing the syntax, or programming rules, of the Java programming language. All Java source files end with the extension .java .

The Java command runs the HelloWorld.class executable in the Java Virtual Machine (JVM). The following output is now visible on your computer display:

 Hello World

Congratulations, you’ve just run your first Java program!

Section 2 - Analyzing the HelloWorld Program

Let’s look at HelloWorld.java line by line to understand how it works. Line 1 contains the following:

public class HelloWorld 

All Java source is contained within a class , which is simply a user-defined type. The name of the class can be any valid Java identifier (discussed in the Variables lesson). We have given our class the name HelloWorld . 

The keyword public is a modifier, which makes the class visible to the whole world. Java source files can have only one public class. If the source file contains a public class, the name of the class must match the name of the source file (public class HelloWorld can only exist inside file   HelloWorld.java ).  

Scope refers to the region of memory in which a construct is defined. In Java, the scope is defined using curly braces. The open curly brace character, {, begins a scope, while the close curly brace character, }, ends a scope. The scope of class HelloWorld is defined within the open curly brace character on line 2 of Listing 1 and the close curly brace character on line 7 of Listing 1. 

Line 3 of HelloWorld.java contains the following:

public static void main(String[] args)

A statement directs your program to take a single action. All the work that your computer program does, referred to as code , consists of a series of statements. In Java, all statements end with the semicolon character (;) . All statements are organized within methods , which are simply series of statements which are grouped together in order to perform a common task. All methods are defined inside classes, and the name of the method inside class HelloWorld is main .

The main method is special in Java. When you run the HelloWorld program using the Java command, the JVM looks for a method called main , and executes it.

You can pass information to a method, and the method can use this information internally. We use the term parameters to refer to this information. Parameters are specified immediately after the name of the method within parentheses. The main method has parameter String[] args , which is a Java array of String references named args (discussed in the Methods and Arrays lessons).

The main method begins with the public modifier. This states that main can be called outside the class HelloWorld . The static keyword specifies that we do not need an object to call main (discussed in the Objects and References lesson). The void keyword is the return type of the main method. It specifies that main does not return a value to the JVM. The main method must be specified as on line 3 (except that any name may be used for the args parameter) for your program to compile and run correctly. 

The scope of the main method is specified within the curly braces on lines 4 and 6. Our main program contains the following statement (ending in a semicolon): 

System.out.println(“Hello World”); 

System.out is a stream which represents the standard output of your computer. When running a Java program from the command line, the standard output is displayed in the command window. When running in an Integrated Development Environment (IDE), the standard output is displayed in the IDE’s output window.

System.out is a stream which represents the standard output of your computer. When running a Java program from the command line, the standard output is displayed in the command window. When running in an Integrated Development Environment (IDE), the standard output is displayed in the IDE’s output window.


No comments:

Post a Comment