Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. No Comments on Creating Compiling and Executing a Java Program
0
(0)

In this article, I will explain creating compiling and executing a Java program which prints Hello World on to the console (command prompt in Windows). Before doing this, you should make sure that your system (PC or laptop) is ready for compiling and executing Java programs. Click this link to setup your system to work with Java: Download, Install and Configure to run Java.

Note: This tutorial assumes that your operating system is windows.

First step is to type the Java source code into a text editor and save it with .java extension. Type or copy/paste the below code in your favorite text editor.

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

Above Java program prints Hello World on to the console (command prompt in Windows). After typing the above program save the file as Hello.java. Note that file name Hello is same as the class name. It is good practice to save the file with the same name as class name but is not mandatory. Let’s assume that the file is saved in the path C:/java. So, full path to the source file is C:/java/Hello.java.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Next step is to compile the source code file. Open the command prompt (black window) and navigate to the location C:/java. Type the following command to compile the java file:

javac Hello.java

If there are no errors in the Java program, the program compiles successfully and you will see the prompt again as shown below:

java compile

After compiling the program, you can see the generated file Hello.class which contains the bytecode in the location C:/java.

Next step is to execute the program. To execute the program, type the following command in the command prompt:

java Hello

Take care that .class is not added after the class name Hello.

You can see the output Hello World as shown below:

java-execute

Alright! Now that we have executed our first Java program, we will look closer at the Java program to discuss various elements and their use.

Comments

In Line 1, you can see comments. In Java, we can write comments at any line in the program. There are different types of comments. The one you see in the above program is a single line comment. A single line comment starts with // followed by anything you want to write. Comments are useful for writing something which will be useful for the developer or other users. Remember that Java compiler ignores comments.

class Keyword

In Lines 2 to 8, you can see a class declaration. Don’t worry too much about classes. They will be explained in detail in a separate post. For now remember this. Every Java program must contain at least one class. A class can be declared in Java by using the class keyword.

main Method

In Lines 4 to 7, you can see the main method. Again, don’t worry too much. The main method is an entry point for every Java program. For executing a Java program, we have to write a main method. When JVM executes the bytecode, it searches for the main method and starts the execution from that line.

System.out.println

It’s normal in a program to do computations, reading data from the user and printing the results. Reading data from the user in the console window (command prompt) is called Standard Input and printing/writing data to the console is called Standard Output. In the above Hello World program, I am printing to the console by using println method which is provided by the out object of PrintStream class and the out object is made accessible by System class. Let’s say that whenever we want to print something on to the console, we have to use System.out.println.

System and PrintStream are predefined classes which are provided by Java. All these predefined classes come bundled in packages. A package is a group of related classes. System class is available in java.lang package and PrintStream is available in java.io package.

Remember that I explained the above program in a shallow manner. More details will be provided in other articles.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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