Hello World Program

Hello World Program

Let us start with the first program.

Example No.1:

Com.knowledge2life class HelloWorld { public static void main(String[] args) { System.out.println("Knowledge2life"); } }



Example No.2:

Com.knowledge2life class HelloWorld1 { public static void main(String[] args) { String output = "Welcome to Knowlegde2life"; System.out.println(output); } }


Welcome to Knowlegde2life

Syntax explanation:

    public static void main(String args[ ]): Meaning of every word:

  • public: Everyone can access
  • static: We need not have to create an object of the same class to access the method
  • void: Not returning anything
  • main(): Name of the method : entry point for JVM
  • String[] args: Arguments for the main method
  • args : A Name of the array

Key parameters of Java Hello World

  • class : Keyword used for declaring a Java class.
  • public : It is an access modifier representing visibility. This means that it is globally visible.
  • Static : It is also a keyword. Any method that is declared as static is referred to as a static method. The main advantage of the static approach is that it does not require creating an object to use. Because the JVM executes the main method, there is no need to build any entity to call it. As a result, it conserves memory.
  • Void : It is a method’s return type. This typically means that the method doesn’t return anything.
  • Main : It marks a program’s starting point.
  • String[] args : It is helpful for the command line arguments.
  • System.out.println() : It is used for printing any statement. The system is a class, a PrintStream class object, and println() is a PrintStream class method.

Key takeaways:

  • A class definition is required for every valid Java application (that matches the filename).
  • The main method must be defined within the class.
  • Starting with the main function, the compiler executes the code.