Hello World Program

 

 

In this section i will show you how to write the first program of Java without IDE and with IDE. Before we start i will introduce the basic workflow of how Java programming works; The diagram below describes the workflow of java.

so the procedure is :

  1. using javac provided by JDK to compile *.java file into *.class
  2. using java provided by JDK to let JVM load and run the program

programming without IDE

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 window.
  3. Execute (or run) it by typing “java HelloWorld” in the terminal window.

The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named HelloWorld.class); the third actually runs the program.

  • 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 email. 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) {
          // Prints "Hello, World" in the terminal window.
          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. (We use the % symbol to denote the command prompt, but it may appear different depending on your system.)
    % javac HelloWorld.java
    

    If you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above.

  • Executing (or running) a Java program. Once you compile your program, you can execute it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following in the terminal window:
    % java HelloWorld
    

    If all goes well, you should see the following response

    Hello, World
    
  • Understanding a Java program. The key line with System.out.println() prints the text “Hello, World” in the terminal window. When we begin to write more complicated programs, we will discuss the meaning of publicclassmainString[]argsSystem.out, and so on.
  • Creating your own Java program. For the time being, all of our programs will be just like HelloWorld.java, except with a different sequence of statements in main(). The easiest way to write such a program is to:
    • Copy HelloWorld.java into a new file whose name is the program name followed by .java.
    • Replace HelloWorld with the program name everywhere.
    • Replace the print statement by a sequence of statements.

programming with IDE

In this section i will show you how to use Intellij IDE to build a Hello World program.

 

Step 1: create new project

 

Step 2: Choose Java project and Project SDK choose JDK1.8

step3:create a HelloWorld java file like below and run the program, and you will see Hello World is printed here!

 

Leave a Reply

Your email address will not be published. Required fields are marked *