Monday, January 25, 2016

Hello World Program

Now we will begin creation of the simplest program, namely "Hello World" program. This program is a program that usually first made by programmers in learning a new programming language. Here's a guide to make the program "Hello World":
  1. Create "javacode" directory in D: drive, so you will have D:\javacode directory. We will use this directory to place our Java source code.

  2. Run text editor. In this example, we use Notepad. For those of you who know Integrated Development Environment (IDE) such as Eclipse or NetBeans, we have not used the IDE at this time, because by using Notepad we will understand the basic techniques of Java programming.

  3. Write the following code in it:
    /*
       This is Java code.
       File of this code should be saved with the name HelloWorld.java.
    */
    public class HelloWorld {
       //Execution of the program will be started by calling main().
       public static void main(String[] args) {
          //Print Hello World to the console.
          System.out.println("Hello World");
       }
    }
    
  4. Save with the name HelloWorld.java
In most other programming languages, the name of the file that containing the program code can be saved with any name. However, in Java programming, the file name must exactly match with the name of the class that has the main() method. Thus, for example in the program above, we must save the file with the name HelloWorld.java. The following image display our Java code in Notepad:



Now, let's see more about the program code above. Although it looks a little (only consists of a few lines of code), the above program has several key features that common and is applied to all Java programs. First, the above program begins with the following code:
/*
   This is Java code.
   File of this code should be saved with the name HelloWorld.java.
*/

The above code is a program comment. Program comment is part of a program that will not be translated during the compilation process. The presence of the program comment is only used to provide the required information and will not affect the course of the program. Programmers usually use comment to write down the information about the code: when created, when modified, whom the author, and the most common is to write a logical process (algorithm) of the code created. Java provides three ways to write program comment: using sign /*..*/, //, and /**..*/. Comment that preceded by /* and end in */ may consist of a few lines, while starting with // will only apply to a single line. In code above we can see comment that has only one line, that is:
//Execution of the program will be started by calling main().
and
//Print Hello World to the console.

The third type of comment (started with /** and end with */) are used to create documentation of using javadoc program.

The next line is as follows:
public class HelloWorld {

Line of code above using the class keyword to define a new class and followed by the name of the class. In Java, all programs are classes. Opening curly brackets, {, is used to initiate a program block, while the closing curly brackets, }, used to end a program block. Program block can be any class, method, selection structure, repetition structure, and so on. In this time, we do not discuss about the presence of public keyword contained in the code line. We will review it later in the next chapter that talks about package and interface.

Next, the HelloWorld class contained the following line of code:
public static void main(String[] args) {

The above line would start main() method. The main() method is the primary method to be executed first time when the program starts. If you ever learn C or C++ language, main() method contained in Java will be the same as the function of main() contained in C or C ++.

The next line of the code above is as follows:
System.out.println("Hello World");

The code used to display the "Hello World" text to the console.

Lastly, the above code is closed with closing curly brackets sign, }. The sign is used as the final part of the HelloWorld class definition.


Compile and Run


To compile and run a program that we made, we need to use command line via Command Prompt. The steps are as follows:
  1. Open Command Prompt, and go to our Java code directory in D:\javacode.

  2. Perform compile a file using the command javac HelloWorld.java. If the compilation process runs smoothly without any errors, then system will not display anything in the console, but in that directory system will create a new file with the name HelloWorld.class. This file is called a bytecode and this file is the end file of program that we created.




  3. To run or execute the program, type command java HelloWorld. System will display "Hello World" text to the console, as shown as follows:


12 comments:

  1. Thank you for posting the valuable information about the java .And every people easily understand about your posting, and I am learning a lot of things from your posting,Keep it up.java classes

    ReplyDelete

  2. It is nice article to improve my knowledge.thank you for sharing useful post
    visit
    web programming tutorial

    welookups

    ReplyDelete
  3. Nice blog with excellent information. Thank you, keep sharing.ServiceNow Training in Hyderabad

    ReplyDelete
  4. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!



    apple service center chennai | Mac service center in chennai | ipod service center in chennai | Apple laptop service center in chennai

    ReplyDelete
  5. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    top servicenow online training
    servicenow online training
    best servicenow online training

    ReplyDelete
  6. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Dwarka

    ReplyDelete