How to compile and run your first Java program

This article is intended for the beginner programmers who want to write their first Java program, we will write a simple Java program and describe in details its syntax and the tools you need to compile and run it.

Environment Setup

The following tools are required for writing and compiling a Java program:

  • Install JDK (Java Development Kit)
  • Use any editor or IDE of your choice e.g. Eclipse, IntelliJ or Notepad++.

Why do we need to Compile a Java Program?

As a beginner, you might not understand why are we setting up JDK and how does it work? Java Development Kit provides a Java Runtime Environment (JRE) and a Java Virtual Machine (JVM).

A simple Java program is a set of instructions, interpreted to a byte code that runs on a JVM. This practically means “write once, run anywhere”. Any machine having a JVM downloaded will run the byte code written maybe apart from the native development machine. So a java program written on a Windows machine can still run on a Linux machine with JVM installed.

Let’s Code!

Here’s a small snippet of code, you can copy and paste in any editor to run and see what happens!

My First Program:



  • class MyFirstJavaProgram {:

“class” is a reserved keyword in Java, that is specific to declare a class. A class in simple words is a template or a blueprint for an object(s) to possess certain attributes. This is a relatively high level concept that you will understand in later lessons. For now, just remember all java programs consist of one or several classes. The keyword “class” is followed by its name “MyFirstJavaProgram”.

The opening curly brace “{“ is a part of Java syntax, and marks the beginning of scope of the class “MyFirstJavaProgram”. Each opening brace “{“ will be accompanied by an ending brace “}” that will mark the end of the class.

  • // Hey! This is my first Java Program

“//” in Java is used to represent a “comment”. It means that the compiler will treat everything stated after double slashes (//) as a “plain text” and not code till the end of that line. Commonly, we use comments to add human readable instructions for other developers to understand the functionality of the program.

  • public static void main(String[] args) {

“public” is an another keyword used as an access modifier. By using an access modifier like “public”, you are specifying that the “main” method can be accessed from outside the class.

“static” keyword allows you to call the “main” method without creating an object of the class

The reserved word “void” defines the return type of the method “main”. Every method you define has to have a return type. As the word elaborates itself, “void” means null/empty so this method does not return anything.

“main” is the name of the method you have defined. A method in Java is a bunch of instructions that are generic enough to perform a certain function multiple times. We use methods to save development time and space to write same set of instructions more than once. For example, if you have to print any paragraph or sort two integers, a predefined method you write once should be reused to print any paragraph or to sort any two integers later on. If you have familiarity with other programming languages, you might have came across “methods” as “functions”.

“String[] args” – We are passing an array of Strings named “args” to the main method as a parameter. Since methods are supposed to be generic, we can pass any parameters to it depending on its functionality.
“{“ marks the beginning of the scope of “main” method.

  • System.out.println(“Hello World!”);

This is a built-in functionality provided by java to print anything enclosed in the double quotes on a new line in console (“your printable text goes here”). The semicolon “;” placed at the end of the line is a part of the java syntax that acts similar to what a full stop does in English language punctuation.
The last two closing braces formally end the scope of the “main” method and “MyFirstJavaProgram” class respectively. Note that, the brace opened latest closes first and so on.


At this point, we assume you’re able to print your first “Hello World!” in Java. But it’s alright if you do not completely understand the keywords and their functionalities. You can always refer back to this article or for a more deep elaboration hop on to Java Syntax Guide by Code Gym.

0 0 vote
Article Rating

Hussein Terek

Founder of, I have a passion in software engineering and everything related to java environment.

You may also like...

Inline Feedbacks
View all comments