Hello World Program in Java

Hello people, To take your baby steps in java programming, knowing the fundamentals is unskippable. In this article, we will learn the foundation program in Java. If you consider any programming language, the Hello World program is a popular one to start learning. So, let us begin with Java hello World Program.

Prerequisites of Java Programming:

The requirements are listed below:

  • Install JDK(Java Development Kit)
  • Set environment variables

Now you are good to go.

Basic steps in Java Programming:

Let us look into some basic steps in Java programming:

1. Write your program in a text editor and save the file as filename.java.

2. Open the command prompt and use the following command to compile it.

javac filename.java

3. To run the program, use the command given below.

Java filename.java

You can also use IDE to compile, run and debug the java programs. The Integrated Development Environment(IDE) is the one-stop shop for all your programming needs. It consists of a text editor, compiler, debugger, runner, and other essential tools with Graphical User Interface. Some popular IDEs for Java are Eclipse, Netbeans, IntelliJIDEA, etc.

Internal process in Java Programming:

Java Development Kit(JDK) is the development environment for developing applications or software in Java. It comprises Java Runtime Environment(JRE), interpreter/loader, documentation generator, compiler, archiver, and other development tools.

Java Runtime Environment(JRE) is an environment that runs the java program. It comprises a Java Virtual Machine(JVM) and a set of library files.

JVM is a runtime interpreter that helps in the line-by-line execution of java programs. It comprises the class loader, bytecode verifier, JIT compiler, and interpreter.

internal process in java programming Now let us begin to know the internal process in Java programming.

1. You will develop your program files using a text editor or IDE.

2. When you compile the program, the compiler in JDK converts the source program into a bytecode or class file.

3. When you run the program, It enters the Java Runtime Environment. Now the JVM takes charge and uses the

4. class loader to load the main class and all the necessary classes.

5. Then the Bytecode verifier verifies the bytecode for any misleading instructions or illegitimate code.

6. Now the Jit compiler converts the bytecode to machine code at runtime.

7. The interpreter uses machine code to execute the program.

8. Finally, JRE executes the program successfully.

internal process in java hello world programming

Java Hello World Program:

Consider the following program:

/* This a Hello World program
File Name: FirstCode.java */
public class FirstCode {
    //Main method. This method prints the given text on the terminal.
    public static void main(String[] args)
    {
    System.out.println("Hello World! Welcome to FirstCode");
    }
}

Compiling java hello world program:

To compile and run the above program, Follow the steps given below:

1. Open the command prompt.

2. Navigate to the folder that contains your java file using the cd command.
For example, cd C:\DataFlair\FirstFolder.

3. Enter the command mentioned below to compile the program.

Javac FirstCode.java

Now the FirstCode.class file is created.

4. Use the following command to run the program.

java FirstCode.java

Output:

Hello World! Welcome to FirstCode

hello world java

Explanation:

Class:

The following line of the program represents the declaration of the class.

Code snippet:

public class FirstCode{
//action block
}

Here, public is the access modifier that represents the accessibility of the class. Public means that it is accessible from anywhere. This access modifier is optional.

For representing a class, we use the keyword class. FirstCode is the name of the class. The class name’s first letter must always be in uppercase.

Main method:

The following line of the program represents the declaration of the method.

Code Snippet:

public static void main(String[] args){
//action block
}

Here the public is the access modifier that represents the accessibility of the method. So it is accessible from anywhere.

The keyword static means we can invoke the method without creating any object. Usually, we use an object to use a method in Java. For example, JVM invokes the main method. It is the entry point in a java program. Therefore, there is no need to create an object for using the main method to save memory.

The keyword void is the return type of the method. That means it does not return any value.

“main” is the method name that serves as the entry point in a java program.

String[] args is the argument of the main method for getting command line arguments. Here String represents the data type, args is the argument name, and [] means an array. There are two methods to illustrate the arguments.

They are
Method 1 : String[] args

Method2: String args[]

Both the above methods perform the same functionality. Command line arguments are nothing but the arguments passed while running a java program from the console.

Print:

The line below prints the given text in the output terminal.

Code Snippet:

System.out.println("Hello World! Welcome to FirstCode");

The built-in method println takes the text within the quotes as an argument. The println method prints the values in the terminal.

Comments in Java:

Java Comments are of two types. They are

1. Single-line comments:

Consider the above program. The line which starts with two forward slashes(//) is considered a single-line comment. That means the compiler ignores this line, and it will not have any effect on the program. It is mainly used for documentation purposes and to improve program readability.

For Example:

//Main method. This method prints the given text on the terminal.

2. Multi-line comments:

In the above program, the line that starts with /* and ends with */ is considered a multi-line comment. Like single-line comments, Java ignores these lines and does not execute them. It is also used to make the code more readable and for proper documentation.

For Example:

/* This a Hello World program
File Name: FirstCode.java */

Generally, apply single-line comments for short comments and multi-line comments for longer ones. It is up to the programmer to decide between single-line or multi-line comments.

Points to remember:

Some points to remember while writing code are:

1. Always follow code standards and rules to create a more understandable program.

2. Follow naming conventions like meaningful names and CamelCases for variables, methods, classes, etc.

3. Use comments wherever necessary.

4. Proper indentation is a must.

5. Avoid unnecessary object creation.

Summary

We have taken baby steps into the java programming world by learning the basics. I hope you gained some knowledge. Consistent practice is needed to master coding skills. So no more delay, fold your sleeves and start working.

Leave a Reply

Your email address will not be published. Required fields are marked *