Skip to main content
Advertisement

Ch 1.3 Writing Your First Java Program

Let's now learn how to write and execute Java code in practice.

1. Hello.java

This is the most basic "Hello World" program.

public class Hello {
public static void main(String[] args) {
// Output text to the console
System.out.println("Hello, Java LLC!");
}
}

🔍 Code Explanation

  • public class Hello: In Java, all code must reside within a class. The class name (Hello) must exactly match the file name (Hello.java).
  • public static void main(String[] args): This is the application's ** Entry Point**. When a Java program starts, it specifically looks for the main method.
  • System.out.println(...): A command that tells the system to print text to the console.

2. Java Execution Process

Execution involves two distinct steps:

  1. Compilation: Using the javac Hello.java command, human-readable code is converted into bytecode (.class files) that the JVM understands.
  2. Execution: Using the java Hello command, the JVM executes the compiled bytecode.

3. Comments

Comments are used to write notes or explanations. They do not affect the program's execution.

  • Single-line comment: Everything following //.
  • Multi-line comment: Everything contained between /* and */.

4. Common Errors for Beginners

  • Missing Semicolon (;): Every statement in Java must end with a semicolon.
  • Case Sensitivity: Java treats main and Main as different entities.
  • Filename Mismatch: If the class name and the file name don't match, a compilation error will occur.

tip

In IntelliJ IDEA, typing psvm and hitting Tab automatically generates the main method. Similarly, typing sout followed by Tab expands to System.out.println.

Advertisement