Here's How to Compile and Run a Java Helloworld Program

AnanthaRamu MP
0

Here's How to Compile and Run a Java Helloworld Program

Courtesy: Pixabay

Introduction

In this section we will understand
  1. How to compile a Java Program
  2. How to run a Java Program
  3. Understand Java Hello World Program.

Prerequisite

  • Ensure Java is installed on the System.

Create Java Hello World Source File

Open a text editor and enter the following code:


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

}

Save this file as `MyFirstJavaProgram.java`. The program file name should exactly match the class name, and Java is case-sensitive.


Compile the Java Hello World Program

Open a command prompt or terminal window. Navigate to the directory where you saved `MyFirstJavaProgram.java`. Type the following command in command line and press Enter:

`javac MyFirstJavaProgram.java`

If there are no errors in your code, the command prompt will proceed to the next line. This step uses the Java compiler (`javac`) to translate the Java source code into bytecode, which is stored in a `.class` file (e.g., `MyFirstJavaProgram.class`).


Run the Java Hello World Program

In the same command prompt or terminal window, type the following command and press Enter:

`java MyFirstJavaProgram`

You will see "Hello World" printed on the window. This step uses the Java Virtual Machine (JVM) to interpret and execute the bytecode.


Congratulations!. Now that you have run Java Program, Lets Understand the Java "Hello World" Program:


public class MyFirstJavaProgram:

  1. class: In Java, a class is a blueprint or template that describes the behavior and state that objects of its type support.
  2. public: This is an access modifier that means the class is accessible from anywhere.
  3. MyFirstJavaProgram: This is the class name. By convention, class names start with an uppercase letter.

public static void main(String []args):

  1. This is the main method, which is a mandatory part of every Java program, and the Java program processing starts from this method.
  2. public: As with the class, this modifier makes the `main` method accessible from anywhere.
  3. static: This keyword means that the `main` method belongs to the `MyFirstJavaProgram` class itself, rather than to an instance of the class. It can be called without creating an object of the class.
  4. void: This indicates that the `main` method does not return any value.
  5. main: This is the name of the method. Method names typically start with a lowercase letter.
  6. String []args: This declares a parameter named `args`, which is an array of `String` objects. It is used to receive command-line arguments when the program is executed.

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

  1. This statement prints the string "Hello World" to the console.
  2. System: This is a final class in `java.lang` package.
  3. out: This is a static member of the `System` class, which is an instance of `PrintStream`.
  4. println(): This is a method of the `PrintStream` class that prints the given argument to the console and then moves the cursor to the next line.

Comments:

Java supports single-line comments (starting with `//`) and multi-line comments (enclosed within `/*` and `*/`). All characters within comments are ignored by the Java compiler.

Example: `// prints Hello World` and `/* This is my first java program. */`.


I highly recommend compiling and running the program in a NotePad.


Post a Comment

0 Comments

No SPAMS please.Give constructive Feedbacks.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!