The Basic Java Program

A Simple Example of Java Code

© Ana Mills

Dec 1, 2008
helloWorld output, Ana Mills
See how a simple Java program works in a line-by-line breakdown, along with gentle introductions to some more complex concepts.

This article goes right into the working meaning of code and expands to discuss more complex concepts common in Java programming. Learn some background information about Sun Microsystem's Java language to gain a better understanding before reading this tutorial. Introduction to Java Programming discusses some preliminary topics like classes, objects, methods, among others.

The Simple Program

The example program code follows and should run without error when typed into an Integrated Development Environment. Afterwards a line-by-line explanation gives the definition and purpose of each phrase.

class helloWorld{

public static void main(String args[ ]){

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

}

}

The class definition

The first line is a declaration statement to introduce the code to the computer before it is used:

  • class notifies the interpreter that a new class will be defined.
  • helloWorld is the unique name for this new class.
  • The curly braces signal the beginning and end of the class body

Note that this article does not list Java's coding conventions, but a good programmer learns Java Coding Conventions to save time and mistakes.

The main method

The second line starts the 'main method', where the programmer outlines every step of what they want to accomplish. Every Java application program has any number of unique methods, but must also contain at least one and only one main method. Java applets, however, do not contain main methods.

  • public is known as a 'global', a signal to the interpreter that this method can be used in other parts of the program. The private keyword prevents the rest of the program from calling anything inside a method.
  • static tells the interpreter that the main method applies to everything in the helloWorld class, rather than just one element. Breathing and daily activities work as analogies for static and instance methods; daily activities vary but breathing is required.
  • void indicates that this method finishes operating without returning any results.
  • main is the unique identifier for this method and is a reserved word because all Java applications have one and only one main method.
  • The parentheses indicate that parameters can be passed to this method.

The String array

The third line declares an array, an element that stores a list of values like x[1,2,3]. Contrast arrays with variables that store only one value at a time, like x=3. String defines the type of values received: numbers, characters, and symbols. Args is the unique name for this array, and the brackets signal its array status.

The System class & output

The Java Development Kit has many preinstalled packages that consist of classes for common operations; the last line of code is a package reference or link to such a package.

System is a class in Java's default package and handles system hardware. Out is an object of that class and handles output. Println() is a method of System and accepts Hello, world! as a parameter to output to the screen. Interpreters normally ignore spaces in stored values, so quotation marks declare this a string parameter to preserve such characters.

Summary

The first three lines of this program consist of preliminary definitions to set up the final package reference. The program must name itself and define how, where, and what it will do before it can begin processing. Because this is a tedious process, Java has many prewritten classes or packages that can simply be referenced in a new program.


The copyright of the article The Basic Java Program in Javascript/Java Programming is owned by Ana Mills. Permission to republish The Basic Java Program in print or online must be granted by the author in writing.


helloWorld output, Ana Mills
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo