Java - First Program Notes
Introduction:
Every Java program starts with a class definition. The class name should match the file name.
Basic Structure of a Java Program:
public class ClassName {
public static void main(String[] args) {
// Code to execute
Example: Hello World Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
How It Works:
- `public class HelloWorld`: Declares a class named HelloWorld.
- `public static void main(String[] args)`: Entry point of the program.
- `System.out.println(...)`: Prints text to the console.
Steps to Compile and Run:
1. Save the program as HelloWorld.java
2. Open terminal/command prompt and navigate to the file location.
3. Compile: javac HelloWorld.java
4. Run: java HelloWorld
Output: Hello, World!
Important Points:
- Java is case sensitive.
- The file name must match the class name.
- The `main` method is mandatory for execution.