These tutorials will introduce you to Java programming Language. You'll compile and run your own Java application, using Sun's JDK. It's extremely easy to learn java programming skills, and in these parts, you'll learn how to write, compile, and run Java applications. Before you can develop corejava applications, you'll need to download the Java Development Kit (JDK).


PART-1


Java Hello World Program

Our first application will be extremely simple - the obligatory "Hello World". The following is the Hello World Application as written in Java. Type it into a text file or copy it out of your web browser, and save it as a file named HelloWorld.java. This program demonstrates the text output function of the Java programming language by displaying the message "Hello world!". Java compilers expect the filename to match the class name.

A java program is defined by a public class that takes the form:

 public class program-name {
            
                optional variable declarations and methods
                
                public static void main(String[] args) {
                   statements
                }
                
                optional variable declarations and methods
          
            }

Source Code

In your favorite editor, create a file called HelloWorld.java with the following contents:

/** Comment
 * Displays "Hello World!" to the standard output.
 */

class HelloWorld {

  public static void main (String args[]) {

    System.out.println("Hello World!");   //Displays the enclosed String on the Screen Console

  }
  
}


To compile Java code, we need to use the 'javac' tool. From a command line, the command to compile this program is:

javac HelloWorld.java

For this to work, the javac must be in your shell's path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code.

You're ready to run your first Java application. To run the program, you just run it with the java command:

java HelloWorld

Sample Run

Hello world!

The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial.

HelloWorld.java


Note: It is important to note that you use the full name with extension when compiling (javac HelloWorld.java) but only the class name when running (java HelloWorld).

You've just written your first Java program! Congratulations!!

Next: Java Comments