Physics Wallah

How to Write Your First Java Program in IntelliJ IDEA?

Writing your first Java program in IntelliJ IDEA is a simple way to begin learning programming. Create a Java project with a configured JDK, add a Java class named Main, and write a main() method containing a System.out.println() statement. Run the program using IntelliJ IDEA’s Run option to see the output in the console. Practising basic syntax, understanding error messages, and experimenting with simple code examples can help beginners build confidence in Java development.
Write Your First Java Program in IntelliJ IDEA

If you are starting with Java, the first program you write will probably be a very small one. That is exactly how it should be.

Before getting into loops, arrays, classes or data structures, you need to get comfortable with the basic process of creating a Java file, writing some code and running it.

In this tutorial, we will write a simple Java program using IntelliJ IDEA. We will start by printing a message on the screen and then build on the same program to understand variables, calculations and user input.

By the end, you will have written a few small Java programs rather than just looking at Java syntax in theory.

Start by Creating Your First Java Project in IntelliJ IDEA

Open IntelliJ IDEA and create a new Java project.

Once the project is created, you will see the project files in the left panel. This is where you will create your Java class.

Create a new Java class and name it Basic.

IntelliJ IDEA will create a file called:

Basic.java

You will also see the class structure in the editor:

public class Basic { 

}

There is a simple rule to remember here. If the class is declared as public, its name should match the file name. So Basic.java contains the Basic class.

Set Up the Main Method Where Your Java Program Begins

A Java application needs a starting point. For a basic Java program, that starting point is the main method.

Add the following inside the class:

public class Basic {

 public static void main(String[] args) {

    }

}

There is a lot of syntax in those few words, and you do not need to memorise all of it immediately.

For now, remember one thing: when you run this program, Java starts executing the code inside the main method.

IntelliJ IDEA can also help you generate this structure instead of making you type everything from scratch.

Write Your First Java Statement and See It Run

Now add a print statement inside main:

public class Basic { 

    public static void main(String[] args) {

        System.out.println("Hello World");

    }

}

Run the program from IntelliJ IDEA.

The output will appear in the console:

Hello World

That is your first Java program.

System.out.println() is used to display information in the console. The text you want to display goes inside quotation marks.

Try changing the message:

System.out.println("I am learning Java");

Run the program again and check the output.

You can also use more than one print statement:

System.out.println("Hello World");

System.out.println("I am learning Java");

System.out.println("This is my first program"); 

The console will display each message on a separate line.

Why the Semicolon Matters in Every Java Statement

You will notice that Java statements usually end with a semicolon.

For example:

System.out.println("Hello World"); 

Do not leave it out.

If you write:

System.out.println("Hello World")

IntelliJ IDEA will flag the statement as an error.

You will make mistakes like this while learning Java. That is normal. The useful habit is to read the error indication in the editor instead of immediately rewriting the entire program.

Move Beyond Hello World by Storing Information in Variables

Printing a message is a good first exercise, but programs become useful when they can store and work with information.

Java uses variables for this.

Suppose you want to store someone's age:

int age = 20;

Here, int is the data type, age is the variable name, and 20 is the value stored in it.

You can print the variable just like you printed text:

public class Basic {

    public static void main(String[] args) {

        int age = 20;

        System.out.println(age);

    }

}

The output will be:

20

You can create several variables in the same program:

int firstNumber = 10;

int secondNumber = 20;

System.out.println(firstNumber);

System.out.println(secondNumber);

Now the program is storing information rather than simply displaying fixed text.

Use Variables to Make Your Java Program Perform Calculations

Java can also perform calculations using variables.

For example:

int firstNumber = 10;

int secondNumber = 20; 

System.out.println(firstNumber + secondNumber);

The output is:

30

You can use the other basic arithmetic operators in the same way:

int a = 20;

int b = 5;

System.out.println(a + b);

System.out.println(a - b);

System.out.println(a * b);

System.out.println(a / b);

System.out.println(a % b);

The last operator, %, gives you the remainder.

For example:

System.out.println(10 % 3); 

produces:

1

because 10 divided by 3 leaves 1 as the remainder.

These operators form the basis of many beginner Java programs. 

Make Your Program Interactive by Taking Input From the User

Until now, all the values have been written directly into the program.

That is not very practical if you want to build an interactive program.

For example, suppose you want to create a calculator. You would not want to change the Java code every time you want to calculate a different pair of numbers.

This is where user input comes in.

Java provides the Scanner class for reading input from the console.

First, add this line at the top of the program:

import java.util.Scanner;

Then create a Scanner object inside main:

Scanner sc = new Scanner(System.in);

You can now ask the user to enter a number.

System.out.print("Enter a number: ");

int number = sc.nextInt();

nextInt() reads an integer entered by the user and stores it in the number variable.

Build a Simple Java Calculator That Takes User Input

Let's put these pieces together.

import java.util.Scanner; 

public class Calculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");

        int firstNumber = sc.nextInt(); 

        System.out.print("Enter second number: ");

        int secondNumber = sc.nextInt();

        int sum = firstNumber + secondNumber;

        System.out.println("Sum = " + sum);

    }

}

Run the program.

The console will ask:

Enter first number:

Suppose you enter 10.

It will then ask for the second number. Enter 20.

The final output will be:

Sum = 30

Notice what changed from your first program.

The program now waits for information from the person running it. It stores that information in variables, performs a calculation and then prints the result.

That is a much better example of what programming is about.

Change the Logic and Turn the Same Program Into a New Calculator

Now change the program so that it calculates the product of two numbers.

Replace:

int sum = firstNumber + secondNumber;

with:

int product = firstNumber * secondNumber;

Then change the output:

System.out.println("Product = " + product);

You can do the same thing with subtraction or division.

This is a good exercise at this stage because you are not learning a completely new concept. You are taking the same program and changing how it processes the input.

Put Your Java Basics Together With a Real Calculation

Let's take this one step further with a small mathematical program.

Suppose you want to calculate the area of a circle.

The formula is:

Area = π × radius × radius

You can write the Java program like this:

import java.util.Scanner;

public class AreaCircle {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in); 

        System.out.print("Enter radius: ");

        double radius = sc.nextDouble();

        double area = Math.PI * radius * radius;

        System.out.println("Area of circle = " + area);

    }

}

This time we are using double instead of int because the radius and the calculated area can contain decimal values.

When you run the program, enter a radius such as 5.

The program will calculate the area using the value you provided.

Take a Closer Look at What Each Part of Your Java Program Does

At this point, it is worth looking at the calculator program again:

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");

        int firstNumber = sc.nextInt();

        System.out.print("Enter second number: ");

        int secondNumber = sc.nextInt();

        int sum = firstNumber + secondNumber;

        System.out.println("Sum = " + sum);

    }

}

There are several basic Java concepts in this one example.

The import statement brings the Scanner class into the program.

The class defines the Java class.

The main method is where the program starts.

The Scanner object reads input.

The int variables store whole numbers.

The nextInt() method reads an integer from the console.

The addition operator calculates the sum.

The println() statement displays the result.

You do not need to memorise all of this after writing the program once. Write it, change it and run it a few times. The syntax will become familiar through use.

Running Into Errors? Check These Common Java Mistakes First

When you start writing Java programs, most early errors are small syntax mistakes.

One common mistake is forgetting the semicolon:

System.out.println("Hello World")

It should be:

System.out.println("Hello World");

Another is using the wrong class name.

If your file is Basic.java, your public class should be:

public class Basic

Java is also case sensitive.

These two names are different:

age

and:

Age

The same applies to Java keywords and method names. System.out.println() is not the same as system.out.println().

Variable names also need to follow Java's naming rules. A name such as first number will not work because spaces are not allowed in a variable name. Using firstNumber is a valid alternative.

Practise These Small Java Programs Before Moving Ahead

Do not move straight into advanced Java topics just because you have managed to run Hello World.

Spend some time writing small programs.

Try these:

Write a program that takes two numbers and prints their sum.

Modify it to print their difference, product and quotient.

Write a program that finds the remainder when one number is divided by another.

Take the radius of a circle as input and calculate its area.

Take marks for a few subjects and calculate the total and percentage.

These exercises are simple, but they give you practice with the basic building blocks of Java: variables, data types, operators, output and input.

What Should You Learn Next?

Once you are comfortable creating a class, using variables, performing calculations and taking input with Scanner, you are ready to build on these basics.

The next step is to learn conditions and loops, followed by arrays, methods and object-oriented programming. If you are planning to use Java for software development or technical interviews, you can then move into data structures and algorithms.

This is where a structured Java and DSA learning path from PW Skills can help you take the concepts you have learned here into problem-solving and more practical programming.

For now, though, do not worry about writing complex programs. Spend some time changing the examples in this tutorial, try different inputs and build a few small programs of your own. The more you practise, the more familiar Java's syntax and structure will become.

Your First Java Program Is Small, but the Foundation Matters

You started with a simple Hello World statement. From there, you learned how to create a Java class, write the main method, store information in variables, perform calculations and take input from a user.

These may seem like small steps, but they are the building blocks you will keep using as your programs become more complex.

So before moving ahead, try writing one small Java program without following the examples above. Give it an input, make it perform a calculation and display the result.

Once you can do that on your own, you are ready for the next step in your Java journey

avatar

Get Free Counselling Today

and Clear up all your Doubts

Talk to Our Counsellor just by filling out the form.
Student Name
Phone Number
IN
+91
OTP
Join 15 Million students on the app today!
Point IconLive & recorded classes available at ease
Point IconDashboard for progress tracking
Point IconLakhs of practice questions
Download ButtonDownload Button
Banner Image
Banner Image
Free Learning Resources
Know about Physics Wallah
Physics Wallah is an Indian edtech platform that provides accessible & comprehensive learning experiences to students from Class 6th to postgraduate level. We also provide extensive NCERT solutions, sample paper, NEET, JEE Mains, BITSAT previous year papers & more such resources to students. Physics Wallah also caters to over 3.5 million registered students and over 78 lakh+ Youtube subscribers with 4.8 rating on its app.
We Stand Out because
We provide students with intensive courses with India’s qualified & experienced faculties & mentors. PW strives to make the learning experience comprehensive and accessible for students of all sections of society. We believe in empowering every single student who couldn't dream of a good career in engineering and medical field earlier.
Our Key Focus Areas
Physics Wallah's main focus is to make the learning experience as economical as possible for all students. With our affordable courses like Lakshya, Udaan and Arjuna and many others, we have been able to provide a platform for lakhs of aspirants. From providing Chemistry, Maths, Physics formula to giving e-books of eminent authors like RD Sharma, RS Aggarwal and Lakhmir Singh, PW focuses on every single student's need for preparation.
What Makes Us Different
Physics Wallah strives to develop a comprehensive pedagogical structure for students, where they get a state-of-the-art learning experience with study material and resources. Apart from catering students preparing for JEE Mains and NEET, PW also provides study material for each state board like Uttar Pradesh, Bihar, and others

Copyright © 2026 Physicswallah Limited All rights reserved.