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");
}
}
OUTPUT:
Example No.2:
Com.knowledge2life
class HelloWorld1 {
public static void main(String[] args) {
String output = "Welcome to Knowlegde2life";
System.out.println(output);
}
}
OUTPUT:
Welcome to Knowlegde2life
Syntax explanation:
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.