How to Write a Simple Java Hello World Program
Java is one of the most widely used programming languages in the world. It is a fully object-oriented language and provides users with the capability to develop variety of applications ranging from desktop applications, dynamic web pages, JSP to enterprise-level network applications. The advent of the Android era has revitalized the need for learning Java language as android is powered by Java. Another important benefit of Java is that it can be run on almost all platforms unlike Microsoft’s technologies that target only Windows platforms. The development of the Java language was based on the principle of WORA (Write Once Run Anywhere). This tutorial explains the basic features of a Java Hello World program.
Want to take a detailed course on Java? Take a course at Udemy.com.
Setting Up the Environment
Like most of the other programming languages, before you start writing a Java program, an environment has to be setup. To check whether a system is capable of compiling a Java program, ‘javac’ is the command that can be run in your command line. If the output is similar to the following command prompt screen shot, it means Java is not previously installed on the machine and Java programs cannot be compiled on the system:
Installing Java
The easiest and most convenient way to download and install Java is downloading it from the official website. Go to the following link and download any version of Java SE (Standard Edition) depending on your operating system.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Download Windows x86, or x64 based version if the machine is x86 or x64 respectively. Or if the machine is Linux based, download corresponding Linux version. This tutorial installs a Java version “jdk1.6.0_45” on x64 Windows based platform.
The file download from the link mentioned is in the form of an .exe and has to be opened to install Java on the machine. Once installation completes, go to the following path on the machine:
C:\Program Files\Java\jdk1.6.0_45\bin
The version might be different depending upon the version you downloaded. However, if the above path exists, it means Java has been installed. But, still the compiler doesn’t know if Java has really been installed or not. To tell the compiler that Java has really been installed and now the compiler should run Java programs anywhere on the file system, a new environment variable has to be created. The name should be “Path” and the value should be path C:\Program Files\Java\jdk_version\bin, where “jdk_version” is the name of the version of Java you downloaded.
If the “javac” command is typed in the command prompt, long details of the Java version is displayed as shown in the following figure:
To learn more about Java essentials, take a course at Udemy.com.
Writing Java Hello World Program
Any advanced IDE that supports Java can be used to develop a Java application. The best approach for beginners is to write code in a text editor such as Microsoft Notepad to understand the actual underlying workflow of the program. Follow these steps to write and compile a simple Java Hello World program in notepad/notepad++ or any other text editor. (Notepad++, has been used in this tutorial)
Step1
Open a new file in any text editor and copy and paste the following code in that file:
class Hello { public static void main (String args[]) { System.out.println("Java Hello World"); } }
Everything in a Java program has to be in a class. Therefore, the above example starts with keyword “class” followed by a class name which is “Hello” in the above example. This is similar to C#; as a matter of fact, C# borrowed this syntax from java.
The Java compiler needs an entry point to enter and execute a Java program. This entry is provided by the main method or main function. It can be seen that the first line after the class declaration is:
public static void main (String args[])
The above function declaration declares a public function, which is static and has a return type void, with name ‘main’. The values in the round brackets are the parameters that are passed to main method.
Step2
Click on File => Save As and name the file Hello.java.
Unlike other programming languages where code and the file names can be different, the file containing Java code should be saved with the name of the class that contains the main method, followed by the extension .java. For instance, in Step1, the main method was contained by the class named Hello, therefore the file which contains this class must be saved with name Hello.java (Class name followed by .java extension). Save the file in any directory. (D in this tutorial)
Step3
Open the command prompt and locate to the directory where Hello.java file is located (D directory in this turorial). Type “javac Hello.java” in the command prompt as shown in following figure:
The javac command stands for Java Compiler and it compiles the source file (Hello.java) into compiled file which can be interpreted by the Java Virtual Machine. If directory where Hello.java is located is checked, a new file would be present there with name Hello.class. This is basically the compiled code. This file can be run on any platform (i.e. Windows or Linux) without making any changes in the code. This is where the principle of WORA (Write Once Run Anywhere) comes into play.
Step4
To compile the code, open the command window and locate the directory where the Hello.class file is generated in the last step (D directory in this tutorial). Type “java Hello” in the command window and see the output. The following output would be displayed:
A message “Java Hello Word” has been displayed on the console screen. This is due to the reason that in the main method of the Hello class, the System.out.println method is the method that writes the string passed to the console output and the string passed to it in the code is “Java Hello World” (see step 1)which has been displayed.
For more interesting Java tutorials, check out Java courses at Udemy.com.
Recommended Articles
Top courses in Java
Java students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.