Recents in Beach

How to print Hello World in Java ?



In this tutorial,we will learn How we can write Hello world programs in java.The program will print the simple output “Hello world” on the screen .

So let’s explore how the Java “Hello world” program works .

Note : If you want to run the program on your computer then you must make sure that you have successfully installed java on your machine and have a text editor like sublime , Visual studio code or notepad .

Java “Hello World” program

/*
 * My Fist Program of java
 */

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

Copy the exact code above and paste in your reliable editor ,save the file as  HelloWorld.java  (It is because in java the name of java class and file should be same)


When you compile the above code in your terminal window / command prompt you will get the output “Hello World” on your screen


How Java "Hello, World!" Program Works?

1 . // My First Program on java :  If you are aware of C/C++ then you are also familiar with comments . The statement which starts with // are termed  as comments , if there is no impact of comment in a program . It is used to describe the functionality of the program or for which purpose the program is made .


2 . public class Hello World :  In java every program has a class name and it’s definition without class name you cannot execute the program in java .

In the above program the class name is HelloWorld and it’s definition is written inside it .


public class HelloWorld {

// statement or its definition

}

Please Note  that every Java application has a class definition, and the name of the class should match the filename in Java.

2 . public static void main(String[] args) { ... } : 

This is the main method or entry point. Every application in Java must contain the main method. The Java compiler starts executing the code from the main method.


  • public again means that anyone can access it. 

  • static means that you can run this method without creating an instance of Main.

  • void means that this method doesn't return any value.

  • main is the name of the method.

We will discuss the working briefly in later chapters because it is the basic introduction program for newbie



3 . System.out.print("Hello, World!"); :

The following code prints the string inside double quotation marks Hello World to standard output (your screen) this statement should be in main function, which is inside the class definition.


SUMMARY :

  1. Every Java program has a class and the name must match the filename.

  2. The main function is called the entry point and the compiler start’s executing the program from the main function.

  3. String args system.out.println is the String argument / signature of the main function on the other hand system.out.println is used to display text/output on the screen in a new line .


  1. System is a pre-defined class that Java provides us and it holds some useful methods and variables.

  2. out is a static variable within System that represents the output of your program (stdout).

  3. println is a method of out that can be used to print a line.

Don’t worry if you are not able to understand some concepts like class , static , main , string args ,system.out.println we will discuss it one by one in another tutorial . All you need to do is to subscribe with email to get notified whenever the post is pulled on our website . 


Please leave comments about the post and ask questions regarding the topic .








Post a Comment

0 Comments