Java – First Program

In this Java tutorial, we will learn how to write your first java program, Hello World in Java. After that, we will also discuss how to compile and run a simple java program with example.

First Program in Java (Hello World)

Let’s start with the first example in java. To write Java code, open your favourite text editor (notepad) and write below code.

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

After writing Java code, you need to save the file with .java extension. In my case, I am going to save this file as Hello.java.

How to Compile and Run Java Program

Now, it’s time to compile and run the java program. Java programs are written in English, but the computer understands only machine(binary) code. Compile java Program is a process to convert Java code into Byte code. Run a Java program means to execute the Byte code and process the output. You have to open the command or terminal window of your computer and move to your java source folder.

  • Compile Java – In this phase, java program compiles (translated) in Byte code and create a file with .class extension, known as a class file.
  • Run Java – In this phase Byte codes convert in machine language and execute the program.

Syntax:

javac filename.java    //compile java program
java className        //run java program

Example:

javac Hello.java
java Hello

Let’s Discuss the Java Code.

Let’s discuss each line of the above first java example step by step.

  1. class Hello – In Java, the program always begins with the declaring a class name. In this line, we are creating a class Hello.
  2. public static void main(String arg[])
    • main() is the first method or entry point of the program. Every Java application must have the main() method to start with.
    • public – Access specifier, a public member can call anywhere.
    • static – Access modifier, a common property for all the objects.
    • void – Return type, it means the main does not return any value.
    • String arg[] – main() method always accept array parameter of String type.
  3. System.out.println() – It is an output statement, it prints the result on the user screen.
  4. { } – You can notice opening and closing curly brackets in the program, it creates a block of code. Every time class and methods must be start and end with curly brackets.

Comments in Java

Proper use of comments in a program gives the more readability to the code, we can explain what is going on our program through commenting. Comments are ignored by the compiler.

Single Line Comment

We can make a single line as a comment by placing // at the beginning of the line.

// this is example of single line comment

Multiple Line Comment

We can create multiple lines as a comment by placing /* at the beginning and */ at the end of the comment.

/* 
example of 
multi-line comment
in Java
*/

Mini Quiz

0%

Java - Quiz 2

1 / 5

Q1. Which method is the entry point of every Java application?

2 / 5

Q2. What is the correct file extension for a Java source file?

3 / 5

Q3. Which command is used to compile a Java program?

4 / 5

Q4. What is the purpose of System.out.println() in Java?

5 / 5

Q5. Which symbol is used to write a single-line comment in Java?

Your score is

The average score is 95%

0%

Mini Project

Mini Project: Personal Greeting Program

Question:

Write a Java program that prints the following information using System.out.println():

  • Welcome to CoderMantra
  • Your Name
  • I am learning Java.
  • This is my first Java program.

Also, add:

  • One single-line comment describing the program.
  • One multi-line comment at the beginning of the program.
  • Save the file as Greeting.java.
View Answer Code
/*
   Mini Project
   Personal Greeting Program
*/

class Greeting
{
    public static void main(String args[])
    {
        // Display welcome messages
        System.out.println("Welcome to CoderMantra");
        System.out.println("Name: Rahul");
        System.out.println("I am learning Java.");
        System.out.println("This is my first Java program.");
    }
}

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top