Saturday, 23 May 2015

First Java Progarm

In this post we will learn writing our First Java Program. At first we break the process of programming in Java into three steps:

   1. Create the program by typing it into a text editor and saving it to a file named, say, HelloWorld.java.
   2. Compile it by typing "javac HelloWorld.java" in the terminal/command prompt window.
   3. Run (or execute) it by typing "java HelloWorld" in the terminal/command prompt window.

Creating a Java program.

A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail. HelloWorld.java is an example program. Type these character into your text editor and save it into a file named HelloWorld.java.


 public class HelloWorld { 
       public static void main(String[] args) { 
          System.out.println("Hello, World");
       }
    }

Compiling a Java program.

 A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version). To compile HelloWorld.java type the boldfaced text below at the terminal. (I use the > symbol to denote the command prompt, but it may appear different depending on your system.)

> javac HelloWorld.java

Executing a Java program. 

Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following at the command prompt/terminal:

>java HelloWorld

If all goes well, you should see the following response

Hello Word



/*************************************************************************
 *  Compilation:  javac HelloWorld.java
 *  Execution:    java HelloWorld
 *
 *  Prints "Hello, World". By tradition, this is everyone's first program.
 *
 *  Output:
 *  Hello World
 *
 *************************************************************************/

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }

}


No comments:

Post a Comment