Search⌘ K
AI Features

Writing Your First Java Program

Explore writing your first Java program by understanding the basic syntax and structure. Learn to print output with the classic Hello World example and get familiar with Java program essentials.

We can do a lot with Java. We can write complex algorithms, build mobile and desktop applications, design cool games, and even make sketches on the console!

But let’s slow down a little. Let’s start with a very basic application to get familiar with the nuts and bolts of a basic Java program.

Hello World! in Java 🌏

As you go on to learn more programming languages in the future (hopefully!), you will encounter more “Hello World!” programs.

Note: The “Hello World” phrase was first used by Brian Kernighan as an introductory example for his language B. The phrase was used to replace “hi!”, which had been used previously in his tutorials. The phrase was later used in the documentation of the famous C programming language, also created by Brian Kernighan. The rest is history!

Apparent from the name, it is nothing but a simple program that writes the greeting “Hello World!” on the screen. So, let’s get to it!

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

YAY! We just ran our first program in Java. 🎊

The program prints Hello World! on the screen exactly how we wanted! Let’s now anatomize this syntax to learn why we need 77 lines just to print something to the screen.

Explanation

Let’s break down the program in parts.

First, we’ll see what is mandatory in a Java program.

  • Look at line 1. We add the keywordA keyword is a word that is reserved by a program because the word has a special meaning.: class. Then, we add the title of the program: HelloWorld in our case.

  • Next, we add {}.

  • At line 3, we copied the line as it is: public static void main( String args[] ). It represents the main method of the program. The main method is where the code starts executing from, i.e., if we have multiple lines of code in our program, the Java compiler will start running from the line succeeding public static void main(String args[]).We will go into the details of each of these keywords in the lessons ahead.

  • Next, we add {} again.

Now it’s time to add the magic in this basic structure. Look at line 5 of the program. It is responsible for actually printing the phrase on the screen. The System.out.println is a Java utility that prints whatever it is given in the parentheses, e.g. "Hello World!", on the screen. You can try replacing the word World with your name for a personalized greeting.

🚨 Note: Don’t play with the double quotes (" ")! They differentiate between the Java code and literal text. To print a literal text, we need to enclose it in the double quotes.

This was theHello World! 🌍 program in Java.


Want something fun?

You might be thinking, “What else can you do in Java?” Indeed, printing a greeting to the console is not the most exciting thing to do. Well, as a teaser, we have produced the following code for you. You may not understand what it is doing or how it works. But, don’t panic. It’s just for fun. For now, run and admire!

Java
import com.educative.graphics.*;
class SmileyDrawing
{
public static void main(String[] args)
{
Canvas c = new Canvas(500,500);
c.fill("#FFFF00");
c.circle(250,250,150);
c.circle(250,250,100);
c.stroke("#FFFF00");
c.rect(150,150,200,140);
c.fill("#000000");
c.circle(200,200,10);
c.circle(300,200,10);
c.triangle(245,250,255,250,250,240);
}
}