Java First Hello World Program


In this chapter, we will discuss how to create, compile and run a Java hello world program.
In this chapter, we write our first Java application. The instructions to create a Java program is applicable to users of Windows Vista, Windows 7, and Windows 8. Instructions.

1. Prerequisites

To write your first program, you'll need:
  • The Java SE Development Kit 8 (JDK 8)
You can download the Windows version now. (Make sure you download the JDK, not the JRE.) Consult the installation instructions.
  • A text editor
In this example, we'll use Notepad, a simple editor included with the Windows platforms. You can easily adapt these instructions if you use a different text editor.
These two items are all you'll need to write your first application.

2. Creating Your First Application

Your first application, HelloWorldApp, will simply display the greeting "Hello world!". Let's follow below three steps to create, compile and run this HelloWorldApp program:
  1. Create a source file
A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
  1. Compile the source file into a .class file
The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.
  1. Run the program
The Java application launcher tool (java) uses the Java virtual machine to run your application.

1. Create a Source File

Let's first, start the editor. You can launch the Notepad editor from the Start menu by selecting Programs > Accessories > Notepad. In a new document, type in the following code:
/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}
Before saving file, let's create a directory named myapplication C drive. Now save the code in a file with the name HelloWorldApp.java. To do this in Notepad, first, choose the File > Save As menu item. Then, in the Save As dialog box: 
By looking into the above source code, let's see what is the meaning of class, public, static, void, main, String[], System.out.println().
  • class keyword is used to declare a class in java.
  • public keyword is an access modifier which represents visibility. It means it is visible to all.
  • static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create an object to invoke the main method. So it saves memory.
  • void is the return type of the method. It means it doesn't return any value.
  • main represents the starting point of the program.
  • String[] args is used for command line argument. We will learn it later.
  • System.out.println() is used print statement.

2. Compile the Source File into a .class File

To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is myapplication on the C drive, type the following command at the prompt and press Enter:
cd C:\myapplication
Now the prompt should change to C:\myapplication>.
Now you are ready to compile. At the prompt, type the following command and press Enter.
javac HelloWorldApp.java
Now that you have a HelloWorldApp.class file, you can run your program.

3. Run the Program

In the same directory, enter the following command at the prompt:
java HelloWorldApp
You should see the following on your screen:
C:\myapplication>java HelloWorldApp
Hello World!
Congratulations! Your program works!

Key points

Caution 

Do not use the extension .class in the command line when executing the program. Use java ClassName to run the program. If you use java ClassName.class in the command line, the system will attempt to fetch ClassName.class.class

Tip

If you execute a class file that does not exist, a NoClassDefFoundError will occur. If you execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead of main), a NoSuchMethodError will occur. 

Note 

When executing a Java program, the JVM first loads the bytecode of the class to memory using a program called the class loader. If your program uses other classes, the class loader dynamically loads them just before they are needed. After a class is loaded, the JVM uses a program called the bytecode verifier to check the validity of the bytecode and to ensure that the bytecode does not violate Java’s security restrictions. Java enforces strict security to make sure that Java class files are not tampered with and do not harm your computer.

Summary

  • The Java source file name must match the public class name in the program. Java source code files must end with the .java extension.
  • Every class is compiled into a separate bytecode file that has the same name as the class and ends with the .class extension.
  • To compile a Java source-code file from the command line, use the javac command.
  • To run a Java class from the command line, use the java command.
  • Every Java program is a set of class definitions. The keyword class introduces a class definition. The contents of the class are included in a block.
  • A block begins with an opening brace ({) and ends with a closing brace (}).
  • Methods are contained in a class. To run a Java program, the program must have a main method. The main method is the entry point where the program starts when it is executed.
  • Every statement in Java ends with a semicolon (;), known as the statement terminator.
  • Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program.
  • In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment or paragraph comment. Comments are ignored by the compiler.
  • Java source programs are case sensitive.
  • Java bytecode can be executed on any computer with a Java Virtual Machine.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments

  1. Interesting post. which i wondered about this issue so thanks for posting and very good article which is a really very nice and useful article. Thank you
    Data Science Course in Noida

    ReplyDelete
  2. I really enjoyed reading this post and keep up the good work and let me know when you can post more articles or where I can find out more on the topic.
    Data Science Online Course

    ReplyDelete
  3. Very educating blog, got lot of information thank you.
    Data Scientist Course in Jaipur

    ReplyDelete
  4. Nice Post thank you very much for sharing such a useful information and will definitely saved and revisit your site and i have bookmarked to check out new things frm your post.
    Data Science Course

    ReplyDelete
  5. Well done for this excellent article. and really enjoyed reading this article today it might be one of the best articles I have read so far and please keep this work of the same quality.
    Data Analytics Course in Noida

    ReplyDelete
  6. Excellent work done by you once again here and this is just the reason why I’ve always liked your work with amazing writing skills and you display them in every article. Keep it going!
    Data Analytics Courses in Hyderabad

    ReplyDelete
  7. I like viewing this web page which comprehend the price of delivering the excellent useful resource free of charge and truly adored reading your posting. Thank you!
    Data Science Certification Course

    ReplyDelete
  8. Just a shine from you here and have never expected anything less from you and have not disappointed me at all which i guess you will continue the quality work. Great post.
    Data Science Training in Gurgaon

    ReplyDelete
  9. Excellent post to make this blog more wonderful, attractive and cool stuff you have. Thank You.
    Data Science Course in India with Placements

    ReplyDelete
  10. I just got to this amazing site not long ago was actually captured with the piece of resources you have got here and big thumbs up for making such wonderful blog page!
    Data Scientist Course

    ReplyDelete
  11. Informative Post. The information you have posted is very useful and sites you have referred was good. Thanks for sharing.
    Data Science Course with Placement

    ReplyDelete
  12. You have done a great job and will definitely dig it and personally recommend to my friends. Thank You.
    Data Science Online Training

    ReplyDelete
  13. Data science is a field that does not restrict the people to come towards it. Data science is not limited to only one area.
    data science training in kanpur

    ReplyDelete

Post a Comment