class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World by TechSarvam");
}
}
Explanation for Simple Java Program
Let’s break down the program step by step:
1. class HelloWorld
- This line defines a class named HelloWorld. In Java, everything must be written inside a class, and the class is the blueprint for creating objects. However, in this case, the program does not require creating objects because it is just printing a message.
2. public static void main(String args[])
- This is the main method, which serves as the entry point for any Java application.
- public: The method is public, meaning it can be accessed from anywhere, and this is required so that the Java Virtual Machine (JVM) can call it.
- static: The method is static, meaning it can be called without creating an instance of the class. This is required because the JVM doesn’t create an object of the class to call the main method.
- void: This specifies that the main method does not return any value.
- String args[]: This parameter allows command-line arguments to be passed to the program when it is executed. The args[] array holds the arguments as strings, though in this program, we are not using it.
3. { } (The block of the main method)
- The curly braces {} define the body of the main method, where the actual logic of the program is written.
4. System.out.println(“Hello World by TechSarvam”);
- This is the statement that prints text to the console.
- System: This is a built-in class in Java that provides access to system resources like input, output, and the environment.
- out: This is a static member of the System class, which represents the standard output stream (typically the console).
- Println: This method prints: This method is used to print a line of text. It takes a string argument and prints it followed by a new line.
- “Hello World by TechSarvam”: This is the string that gets printed to the console.
5. Closing the curly braces
- The closing curly braces } mark the end of the main method and the HelloWorld class.
Output:
When you run the program, it will print the following message:
Hello World by TechSarvam
This is because the program uses System.out.println() to output that string to the console.