Java Hello World Program and Programming Style


In this tutorial, you will learn to write "Hello World" program in Java.

A "Hello, World!" is a simple program that outputs Hello, World! on the Terminal. Since it's a very simple program, it's often used to introduce a new programming language.

Programming Style and Documentation

  1. Appropriate Comments
  2. Naming Conventions
  3. Proper Indentation and Spacing Lines
  4. Block Styles


Appropriate Comments , Naming Conventions

  1. Choose meaning and descriptive names.
  2. Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method compute Area.
  3. Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeArea.
  4. Constants:  Capitalize all letters in constants. For example, the constant PI


Proper Indentation and Spacing

  1. Indentation
  2. Indent two spaces.
  3. Spacing
  4. Use blank line to separate segments of the code.


Programming Errors

  1. Syntax Errors
  2. Detected by the compiler
  3. Run time Error
  4. Causes the program to abort
  5. Logic Errors
  6. Produces incorrect result


Creating Your First Application

A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.
The Java application launcher tool (java) uses the Java virtual machine to run your application.

Create a source file

To create a source file, you have two options:
You can save the file HelloWorld.java on your computer and avoid a lot of typing. Then, you can go straight to Compile the Source File into a .class File.
Or, you can use the following (longer) instructions.

First, start your editor. You can launch the Notepad editor from the Start menu by SelectingPrograms > Accessories > Notepad. In a new document, type in the following code:

 import java.util.*; 
 public class HelloWorld
 { 
     public static void main(String[ ] args){ 
             System.out.println("Hello World!"); 
     } 
 } 


Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is java on the C drive.
In the File name text field, type "HelloWorld.java", including the quotation marks.
From the Save as type combo box, choose Text Documents (*.txt).
In the Encoding combo box, leave the encoding as ANSI.

Compiling Programs

On command line
Bring up a shell, or "command," window. You can do this from the Start menu by choosing Command Prompt (Windows), or by choosing Run and then entering cmd.

The shell window should look similar to the following figure.The prompt shows your current directory. When you bring up the prompt, your current directory is usually your home directory for Windows (the preceding figure.To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is java on the C drive, type the following command at the prompt and press Enter:

cd C:\java


Now the prompt should change to C:\java>

Compile and Run

javac HelloWorld.java 
java HelloWorld 


Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses.
Include your name, class section, instruction, date, and a brief description at the beginning of the program.

Your first application, HelloWorldA, will simply display the greeting "Hello world!".

When you run the program, the output will be:

Hello, World!
Previous Post Next Post