This provides you a and cleaner approach to your first . It explains every single line of code to bring clarity for you.

Learn to Write Your First Java Program

The tutorial has the following sections to help you learn quickly.



Before you paddle up to write your first Java program, make sure you have the Java SE development kit installed on the system.

You can confirm it in many ways:

1) On Windows, open the “Run” window (Press Win+R), type “appwiz.cpl” to launch the “Programs and Features” dialog. Search for the Java SE Development Kit. Or you can directly go to the “C:Program Files” or “C:Program Files (x86)” directory, and check if it has some “jdkX.X.X_XX” folder inside.

2) On Linux or Mac OS, open the terminal (CTRL+T), and run the following commands to check Java availability.

# check if java is installed
whereis java

# check java location
which java

In case, you don’t find Java on your system, then download and install the latest version from the formal Oracle site. Once you have everything set up, then check if the JAVA_HOME variable exists or not.

Open the console window, and issue one of the following commands as per the OS you are using:

# On Windows

# On Linux or Mac OS

By the way, if you don’t have the above variable set, then don’t worry and add one yourself. Adding JAVA_HOME to your environment is not only a good practice but useful too.

a) To do it on Windows, open the “Edit environment variables” dialog and set up the JAVA_HOME pointing to the Java installation directory.

b) To do it on Linux or Mac OS, update the user profile “~/.profile” by adding an entry of JAVA_HOME.

Text Editor or IDE

You can either pick a text editor (like Notepad or Notepad++ on Windows and Vi/Vim on Linux/Mac OS) or an IDE (like Eclipse or NetBeans or CodeBlocks) to write Java code.

It’s an ideal practice to begin using an IDE (Integrated Development Environment) so that you can avoid the headache of manually compiling the code and rectify errors highlighted by the interpreter.

Don’t miss to read the Java Coding Guidelines.


Since , newbie programmers have traditionally used the “Hello World!” as their first programming exercise.

In this tutorial also, we’ll be using it to illustrate the basics of Java programming syntax and code flow.

Write your first Java program

First, launch either a text editor or the IDE that you have chosen. Create a file named as “” and save. Next, here are some thumb rules for you to remember.

1. Every Java program has a main class, and its name must match the name of your Java file (i.e., MyFirstProgram in our case). Also, you need to use “public” access modifier before it.

// Class name should match the name of the Java file
public class MyFirstProgram {

2. As per standard naming conventions, Java classes should start with a capitalized character, and follow the camel case format (in which the first letter of each word in Capital).

// The class name remains in CamelCase format
public class << MyFirstProgram >> {

3. The rules suggest against underscores (_) and dollar signs ($).

Also, note that every main class has an entry point routine called “main.” You place the main business logic in this function.

class MyFirstProgram {
public static void main(String args[]) {

Now, check out the following lines of code and type them into the “” file.


public class MyFirstProgram {
    public static void main(String args[]) {
        System.out.println("Hello World!");

After seeing the above code, you might have observed a few keywords used there. Let’s understand their meaning and purpose.

Public: It is an access modifier indicating that you can call the main from anywhere, even from outside the program.

Static: It is a keyword which means the function can exist and run by itself (without creating an object).

Void: Another keyword, it means that the function doesn’t return any value.

Build and compile your code

Open a terminal window and change the current directory to where your java file is available:

cd /path_to_file/

Once done, the next line to write is:

# Build and compile java program

Sometimes, you get the error that the system didn’t find the “javac” command or it’s an inoperable program or batch file. In such a case, check if JDK exists and its environment variable points to the correct path.

Once successful, the compiler will at that point produce a bytecode file called MyFirstProgram.class. Java Virtual Machine (JVM) interprets and executes this file.

C:>dir MyFirstProgram.*
Volume in drive C has no label.
Volume Serial Number is 9050-B24D

Directory of C:

03/23/ 12:27 AM 434 MyFirstProgram.class
03/23/ 12:27 AM 156
2 File(s) 590 bytes
0 Dir(s) 13,673,246,720 bytes free

The Java programming dialect compiler (javac) pursues source files written in the Java programming dialect and turns them into bytecode. Alternatively, the compiler can likewise process comments found in source utilizing the Pluggable Annotation Processing API.

Execute the Java program

For the final step, use the following command:

# Run the java program
java MyFirstProgram

If you see the output, then you have successfully coded your very first Java program!

C:>java MyFirstProgram
Hello World!


If the insight into the program was too much for once, don’t worry about it. As you make progress with your Java skills, you will start realizing the need for every keyword or access modifier used in your very first program. For the time being, take a moment to appreciate your first ever Java code.

Source link


Please enter your comment!
Please enter your name here