Java Hello World Program

Let's learn the first simple Java program that every programmer practiced in their early programming career. Java beginners often start their programming journey by coding the "HelloWorld" program. In this tutorial, you will learn how to code and run a simple Java program that prints "Hello world" as output.

To start, you need an IDE( Integration Development Environment) on your PC or laptop. IDE helps you write and execute the program. This blog post will explain some important terminology a beginner should learn to understand the Hello World program in Java deeply. 

Steps To Create Your First Java Program

  1. Open your IDE(Eclipse/IntellIj)
  2. Create a class file, for example, HelloWorld.class
  3. Check if any compilation error throws
  4. Run the program

Creating Hello World Example

Task: Create a Java program that prints Hello World.

class FirstProgram{

public static void main(String args[]) {

System.out.println("Hello World"); //print hello world

}}




Horray, we have created a simple program that prints Hello World in the IDE console when the program runs on JVM. 

We have created a class "FirstProgram" and added the main() method to print "Hello World" by System.out.println().

Let's deep dive into the code.

Class

Class is a keyword in Java and it is used to create a class. To build any program in Java language a class is required. A class is a template or blueprint in Java and without a class, no program can be built. It contains data variables and methods which are commonly known as properties. Class keyword with class name is the syntax to create a class. 

class FirstProgram{

}


main() Method 

A Java application always starts its execution from a main method. This approach is known as bottom to top approach. Every java project must have a main method.

  • public keyword is an access modifier and it enables the main method to be accessed from anywhere in the project.
  • static keyword is a non-access modifier and it moves the main() method to the class level from the object level. If a method is in class level then no object is required to call or access the method instead a class can be used to access the static method. Here main() method is directly executed by JVM using static.
  • void keyword is a data type that indicates the method has no value to return. Here void is the return type of main() method which returns nothing.
  • main is the method executed by JVM. 
  • String args[] are arguments of the main method

System.out.println()

It is used to print the argument or value passed to println() in the console. Arguments are passed between parentheses. System is a class, out is an object of PrintStream.class and println() is a method of PrintStream class. The println() method prints a statement and creates a new line.
Here we pass "Hello World" string as an argument to System.out.println() statement.


Post a Comment

Previous Post Next Post
B