Cosmic Module

J

Qubits of DPK

March 15, 2026

Core Java

Layman Explanation

Every Java program starts with a class and a main method. The main method is the front door — JVM knocks here first.

The Hello World Program

java
QUBITS OF DPK
1public class HelloWorld {
2    public static void main(String[] args) {
3        System.out.println("Hello, World!");
4    }
5}

Line-by-Line Explanation

Compilation & Execution Flow

javascript
QUBITS OF DPK
1HelloWorld.java
2     ↓ javac HelloWorld.java
3HelloWorld.class  (bytecode)
4     ↓ java HelloWorld
5Output: Hello, World!

️ Common Mistakes

  • Filename must match public class name exactly (case-sensitive)
  • main must be public static void — any variation = JVM won't find it
  • System.out.println vs System.out.print — println adds newline, print doesn't

Interview Answer

"Every Java program needs a class with a public static void main(String[] args) method as the JVM entry point. The file name must match the public class name. javac compiles to bytecode, and java launches the JVM to execute it."

Interview Questions & MAANG-Level Answers

Q1. Why must the filename match the public class name?
The JVM's ClassLoader finds classes by filename. When a class is public, it's accessible everywhere — so everyone must be able to find it by name. If Car.java contains public class Vehicle, ClassLoader looking for Vehicle.class would fail. Java enforces: one public class per file, filename must exactly match (case-sensitive). Non-public classes have no such restriction since they're package-private.
Q2. What does public static void main(String[] args) mean word by word?
public — accessible by JVM from anywhere. static — JVM can call it without creating an object (no class instantiated at startup). void — returns nothing to JVM. main — the specific name JVM looks for as entry point. String[] args — command-line arguments passed when running the program (e.g., java MyApp arg1 arg2). All five parts are mandatory — changing any one (e.g., private instead of public) means JVM won't recognize it as entry point.
Q3. What is the difference between System.out.println and System.out.print?
System.out.println(x) prints x followed by a newline character \n — next output starts on a new line. System.out.print(x) prints x without a newline — next output continues on same line. System.out.printf("%s is %d", name, age) prints formatted output like C's printf. System.err.println(x) prints to standard error stream (for error messages, not mixed with normal output).
Q4. What happens if you have a syntax error in your Java file?
The javac compiler catches syntax errors at compile time and prints specific error messages with line numbers: HelloWorld.java:5: error: ';' expected. The .class file is NOT generated — you cannot run the program. This is Java's advantage over dynamically-typed languages where errors only appear at runtime. Common compile errors: missing semicolons, unmatched braces, wrong capitalization of String/System, missing return statement.
Q5. Can a Java file have multiple classes?
Yes — a single .java file can contain multiple classes, but only ONE can be public and its name must match the filename. All others must be package-private (no access modifier). The compiler produces a separate .class file for EACH class defined. Example: Animal.java with public class Animal and class Helper produces both Animal.class and Helper.class. Best practice: one class per file for clarity and maintainability.