Page Content

Tutorials

How to write Hello World in Java program?

Hello World in Java

A good starting point for learning Java programming is the “Hello World!” program. The essential methods involved in writing, compiling, and running Java code are mainly illustrated by this straightforward, stand-alone application. It provides detailed instructions on how to run the first program for novice programmers.

Here is the typical “Hello World!” Java program code:

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

Let’s break down this program line by line so we may comprehend its elements:

  • public class HelloWorld { … }
    • This line begins the class definition for a class named HelloWorld. In Java, all program activity occurs within a class, making it the fundamental unit of encapsulation. The class keyword is used to declare that a new class is being defined.
    • public is an access modifier. It means that this class is accessible from any other class.
    • HelloWorld is the name of the class. By convention, Java class names should start with an uppercase letter, and if composed of multiple words, the first letter of each inner word should also be in uppercase (CamelCase). The entire class definition, including its members, is enclosed within curly braces {}.
  • public static void main(String[] args) { … }
    • This is the main method, which is the entry point for all standalone Java applications. The Java Virtual Machine (JVM) looks for and calls this special method to begin executing your program.
    • public: As an access modifier, public indicates that the main() method can be called by code outside its class, which is necessary for the JVM to start the program.
    • static: The static keyword means that the main() method belongs to the HelloWorld class itself, rather than to a specific instance (object) of HelloWorld. This is crucial because main() is called by the JVM before any objects of the class have been created.
    • void: This keyword signifies that the main() method does not return any value. Methods in Java can return values, but main() does not.
    • main: This is the standard name for the method where program execution begins. Java is case-sensitive, so Main is different from main. If it’s mistyped, the Java interpreter will report an error because it cannot find the main() method.
    • String[] args: This declares a parameter named args, which is an array of String objects. This array is used to receive any command-line arguments that are passed to the program when it is executed. Even if no arguments are expected, the empty parentheses are still required.
  • System.out.println(“Hello, World!”);
    • This line is the statement that performs the actual output to the console.
    • System is a predefined class in Java, automatically included in your programs. It provides access to system resources.
    • out is an output stream object within the System class, which is connected to the standard console output. System.out therefore encapsulates console output.
    • println() (short for “print line”) is a built-in method that displays the argument passed to it (in this case, the string “Hello, World!”) followed by a new line character. There is also a print() method that does not add a newline.
    • "Hello, World!" is a string literal. In Java, a line of text or a sequence of characters enclosed in double quotation marks is considered a string.
    • Every statement in Java must end with a semicolon (;). This acts as a statement terminator, signalling the end of a command.
  • Comments in Java
    • The “Hello World!” program frequently contains comments, which are readable by humans but that the compiler ignores entirely. They are employed to describe or clarify how the program works.
    • Comments that are only one line long start with // and go all the way to the end.
    • The beginning of multiline comments is /*, and the ending is */. Any symbol that falls between these two is disregarded.
    • A third type, documentation comments, are used by the Javadoc tool to create HTML documentation. They begin with /** and finish with */.

The Development Cycle of Java Writing, compiling, and executing a Java program usually entails the following steps:

  1. Writing the Program:
    • You write the Java code in a plain text editor (like Notepad, TextPad, or within an Integrated Development Environment (IDE) such as NetBeans or Eclipse).
    • The file must be saved with a .java extension (e.g., HelloWorld.java).
    • It is crucial that the filename matches the class name exactly, including capitalization, because Java is case-sensitive.
  2. Compiling the Program:
    • To convert the human-readable code into machine-friendly instructions, you use the Java compiler, javac.
    • From the command line (or by clicking “Build Project” in an IDE), you would type:
    • If there are no syntax errors, javac creates a new file named HelloWorld.class in the same directory. This .class file contains bytecode, an intermediate representation of your program. Bytecode is not directly executable machine code; instead, it’s an instruction set specifically for the Java Virtual Machine (JVM).
  3. Running the Program:
    • To execute the bytecode, you use the Java application launcher, java.
    • From the command line (or by clicking “Run Project” in an IDE), you would type:
    • Notice that you provide the class name (HelloWorld), not the .class extension. The java command automatically searches for a file with that name and the .class extension.
    • The JVM then interprets and executes the bytecode, producing the program’s output.

Code Output: When executed, the program will display the following output on your console:

Hello, World!

Essential Ideas and Importance The JVM and its bytecode enable Java’s “Write Once, Run Anywhere” (WORA) philosophy. A Java application that has been compiled into bytecode doesn’t need to be recompiled for every operating system; it can operate on any platform (Windows, Mac, Linux, etc.) that has a compatible JVM installed. Comparing this portability to traditional programming languages that need to be recompiled for various systems reveals a big advantage.

Modern programming frequently makes use of Integrated programming Environments (IDEs) such as NetBeans or Eclipse, although basic programs can be created and run straight from the command line. With capabilities like code completion, error detection, and optimized compilation and execution, integrated development environments (IDEs) greatly simplify and expedite the development process.

Essentially, the “Hello World!” program offers a succinct overview of the basic Java grammar, object-oriented ideas (such as classes), and the crucial steps involved in creating Java applications.