IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
    
     Home      Products      Services & solutions      Support & downloads      My account     
 
developerworks > Dashboard > IBM Lotus Domino Web Application Development > ... > Java primer > A simple Java program
developerWorks
Log In   View a printable version of the current page.
Overview Spaces Forums Blogs Podcasts Wikis Exchange
A simple Java program
Added by schenkew@us.ibm.com, last edited by jservais on Apr 03, 2008  (view change)
Labels: 
(None)

 This is a community wiki. Be sure you are logged in to edit, comment, and add pages. Not a member yet? It's free and simple. Click Login in the upper right corner and Register.
View a PDF of the original wiki content produced in March 2008 by Lotus and IBM Redbooks.


Hello World

The most commonly taught program when learning a new programming language is Hello World!, which is shown in the following example.

package com.ibm.test;

import java.util.*;
import java.lang.*;

/**
 Hello world program
 */
public class hello {

	public String myName;

	public static void main(String[] args) {
		System.out.print ("Hello world!");
	}
}

In the previous example, the first line of code is the package statement, which identifies the package to which this class belongs. The package name is important because it is what you need to reference when importing this class into another.

Next is the import statement, which is what you use to reference classes that are in another package. In this case as an example, we have imported the Java Platform java.util package and java.lang package. In the above code example, you do not need these two import statements, because the code base never references any of the classes in either package. When compiling the above code, you might get a compiler warning that the packages are not referenced.

The class statement identifies the class name within the package. It is also important to know that the class name must match the file name of the .java file you are working in.

Within the class statement are properties and methods. The first statement marked as public is the declaration of a property of type String, whose name is "myName:. This is included as an example only and is not referenced in the code base either.

Next is the main program body, designated by the public "main" definition. All Java applications need a main function that is identified as the starting point of the application. The main statement itself has a specific structure and syntax that must be adhered to. In this case, the main function simply prints a statement to the designated system console.

Compiling and running the program

Now you are ready to compile. We remove all extra lines of code from the program and are left with the six line file that is displayed in the following example image.

The Java compiler is included in the Java SDK and is javac.exe. To invoke the compiler, you simply provide the Java source file name as its input. The JVM is manually invoked with java.exe. To run the program, simply type the name of the class file.

You're done!

Java image

Integrated development environment and other development environments

In the previous example, we use one of two methods of Java development. We downloaded the Java SDK, did all of our file creation by using standard text editors such as Notepad or Wordpad, and then manually compiled and ran the application.

The second method is the use of an integrated development environment (IDE). An IDE is a packed program or application that, when istalled, provides  an entirely encapsulated and all-in-one environment for doing development work. It contains a front-end editor that allows you to easily edit, debug, build, package, and sometimes even deploy your application if it contains deployment tools within it.  It frequently does not contain a JDK,  as Sun releases new updates and versions of their JDK on a regular basis.  You must consult the vendor documentation and Web site prior to download to determine if you need to also download a compatible JDK.

A detailed discussion on IDEs is beyond the scope of this primer, but we list a few IDEs here for your consideration:

  • Eclipse: An open source community dedicated to open dedicated to an open development platform, the eclpse.org Ecipse IDE provides both a simple and easy to learn IDE.
  • Sun IDE: Sun Microsystems has its own IDE environment that is downloadable from its website. The IDE comes packaged both wihout the JDK and bundled with a JDK.
  • IBM Rational® Software Architect or Rational Application Developer: Based on Eclipse technology, the IBM robust IDE comes with a rich environment for development of enterprise applications.


    About IBM Privacy Contact