Your First Program
Anatomy of a Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
public class Hello– all Java code lives inside a class. A public class must be saved in a file with the same name:Hello.java.public static void main(String[] args)– the entry point. The JVM starts running here.public→ the JVM can call it from outside the class.static→ it runs without creating an object.void→ it returns nothing.String[] args→ values passed on the command line.
System.out.println(...)prints text and moves to a new line.System.out.print(...)stays on the same line.- Every statement ends with a semicolon
;. Blocks of code go inside curly braces{ }.
Comments
// single-line comment
/* multi-line
comment */
/** Javadoc comment – used to generate documentation */
Case matters! ⚠️
Java is case-sensitive. Main, main and MAIN are three different names, and system.out.println will not compile.
Example
public class Greeting {
public static void main(String[] args) {
System.out.print("Hello, ");
System.out.println("Javi!");
System.out.println("Let's learn Java ☕");
}
}
// Output:
// Hello, Javi!
// Let's learn Java ☕