Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Requirements
3.
Java "Hello, World!" Program
3.1.
Program
3.2.
Compile
3.3.
Run
3.4.
Output
4.
Explanation
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Java Hello World Program

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In this blog, we will learn how to write a simple hello world program in Java. A hello world program is the most commonly used method to introduce a language to a newbie. In this blog, we will also see the basic building blocks of Java, for example, access modifiers, types of functions, etc.

Also Read About, Multithreading in java, and Duck Number in Java.

Requirements

The following software or application must be installed before running any Java program:

  1. Install the Java Development Kit. Download and install the JDK if you haven't already done so.
  2. Set the jdk/bin directory's path (in Windows).
  3. Create a Java program.
  4. Compile and run the Java program.
     

Also check out Addition of Two Numbers in Java here.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Java "Hello, World!" Program

Let us now head over to the program to print “Hello, World” on the output screen.

Program

// HelloWorld.java


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

Compile

Open the command prompt (or terminal in linux) and navigate to the directory where the file HelloWorld.java is saved. Then run the following command to compile the Java program.

javac HelloWorld.java

Run

java HelloWorld

Output

Hello, World!

Explanation

  1. Comments: Any line in Java that begins with // is a comment. Comments are included in the code to help users understand the program's aim and operation. The Java compiler completely disregards it (an application that translates a Java program to Java bytecode).
  2. Keywords: Reserved words are another name for Java keywords. Keywords are specific terms that serve as a code's key. As these are Java's predefined words, they can't be used as variable, object, or class names. We saw a bunch of keywords in the above program:
    1. class: It is used to declare a class in Java. Remember that every Java program has a class definition, and the name of the public class should match the filename in Java.
    2. static: Static keyword is meant for attaching parameters (variables and methods) of a class to the class itself rather than to its objects. As the main() method is static, there is no need to create an object to call it.
    3. void: It is the return type of the method. It means it doesn't return any value.
  3. Access Modifiers: Access modifiers in java are used to set the accessibility of a data member. There are four accessibility modifiers – public, private, default and protected. We have curated a blog specifically for access modifiers here.
  4. System.out.println(): To print a statement in Java, use System.out.println(). System is a class, out is a PrintStream class object, and println() is a PrintStream class method. You can read more about java methods here.

Check out this article - Upcasting and Downcasting in Java

Must Read: Java System Out Println

FAQs

1. How do I run a Java program in Eclipse?
Ans: 

Step 1: Launch Eclipse and select File > New > Java Project from the File menu.

Step 2: Type in the name of the project and click the Finish button.

Step 3: Select the project you created in the Package Explorer (left-hand side of the window).

Step 4: Select New > Class from the submenu when right-clicking on the src folder. Click the Finish button after entering the Class name.

Step 5: Create and save the programme.

Step 6: Now hit Ctrl+F11 or pick Run from the Run menu, or click the Run button.
 

2. Which Java IDE is best for beginners?
Ans: There are various great text editors available for Java Programming, for example, IntelliJ IDEA. BlueJ. Oracle JDeveloper, Eclipse, etc.


3. Can Vscode run Java?
Ans: You can use VS Code to read, write, run, and debug Java source file(s) without creating a project.

Key Takeaways

We discussed the following points in the blog:

  1. A class declaration that matches the filename is required for every valid Java application (class name and file name should be the same).
  2. The main method must be defined within the class.
  3. Starting with the main function, the compiler executes the code.
  4. We saw the role of Access Modifiers and Keywords in Java.
     

Recommended Readings:

 

You can also consider our paid courses such as DSA in Java to give your career an edge over others!

Next article
Prime Number Program in Java
Live masterclass