The Basic Java Program

A Simple Example of Java Code

0 Comments
Join the Conversation
helloWorld output - Ana Mills
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.

Ana Mills, Ana Mills

Ana Mills - Ana Mills is an IT professional and freelance writer, with a Bachelor's of Business Administration in Computer Information Systems and ...

rss
Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 9+4?
Advertisement
Advertisement