PART 1 · LANGUAGE FOUNDATIONS · LEVEL 01

Java Platform & First Program

Begin with the execution model: Java source is compiled into verified bytecode and executed by a Java Virtual Machine. This separation explains portability, runtime checks, class loading and later JVM behaviour.

Level 1 of 24Beginner90–120 minutes8 concept checks
CONCEPTS · MECHANISMS · TRADE-OFFS

Understand the Platform before Memorizing Syntax

Java is both a language and an execution platform. Keeping compiler-time and runtime responsibilities separate prevents many beginner misconceptions.

1. JDK, runtime and JVM

The Java Development Kit contains developer tools such as javac, java, javadoc and diagnostics. The runtime libraries and JVM are what a machine needs to execute compiled Java applications. In modern Java distributions, developers normally install a JDK and use its included runtime.

The JVM is an abstract machine specification with platform-specific implementations. It loads classes, verifies bytecode, manages memory, interprets or compiles hot code and provides services such as exceptions and threads.

  • JDK: build, inspect and run Java software.
  • Runtime libraries: standard classes used by applications.
  • JVM: executes bytecode and manages runtime state.
Important: Do not say Java is completely platform independent. Bytecode is portable, while each operating system needs a compatible JVM implementation.

2. Source to execution

A .java file is compiled by javac. The compiler performs lexical, syntax and type checks, then writes one or more .class files containing bytecode. The java launcher starts a JVM and asks it to run a class containing a valid entry point.

The class loader locates required classes. The verifier checks bytecode safety properties. The execution engine initially interprets instructions and may use a just-in-time compiler for frequently executed methods.

StageInputOutput or responsibility
CompileProgram.javaProgram.class bytecode
LoadRequested classRuntime Class object and metadata
VerifyBytecodeStructural and type-safety checks
ExecuteVerified methodsInterpreted/JIT-compiled machine work

3. Anatomy of the first program

A class groups state and behaviour. public class Program declares a type visible to other code. When the class is public, its source filename must match the class name exactly, including capitalization.

The launcher recognizes public static void main(String[] args) as the conventional entry point. public permits launcher access, static avoids constructing an object first, void means no value is returned, and args holds command-line strings.

  • Java is case-sensitive: String and string differ.
  • Statements normally end with a semicolon.
  • Blocks are delimited by braces.
  • System.out.println writes a value followed by a line break.
Important: A successful compilation proves syntax and type consistency, not that the program produces the intended answer.

4. Compile-time versus runtime failures

A compiler error prevents bytecode creation: missing semicolon, incompatible type or unresolved name. A runtime exception occurs after execution starts: division by zero, invalid index or missing file. A logic error completes normally but produces the wrong result.

Classify the failure before editing. Read the first diagnostic, identify the file and line, then fix the cause rather than changing unrelated statements.

  • Compile error: correct source structure or types.
  • Runtime error: inspect input and execution state.
  • Logic error: compare actual behaviour with the requirement.
RUNNABLE JAVA

Examples Worth Tracing

Predict the result before running each program. Then change one value and explain the new behaviour.

EXAMPLE 01

First executable class

public class Program {
    public static void main(String[] args) {
        String learner = "Bhavya";
        System.out.println("Welcome, " + learner + "!");
        System.out.println("Arguments received: " + args.length);
    }
}
The local reference learner points to a String. Concatenation builds the greeting, while args.length reports how many command-line values were supplied.
Output:
Welcome, Bhavya!
Arguments received: 0
EXAMPLE 02

Predictable numeric output

public class Program {
    public static void main(String[] args) {
        int first = 12;
        int second = 8;
        System.out.println("Total = " + (first + second));
    }
}
Parentheses force numeric addition before String concatenation. Without them, evaluation proceeds left to right after the first String operand.
Output:
Total = 20
INTERACTIVE LEARNING · PROGRAM TRACE

Trace Java's launch sequence

Follow one source file through compilation, loading and execution.

JAVA EXECUTION
1public class Program {
2 public static void main(String[] args) {
3 System.out.println("Hello, Java!");
4 }
5}
VISIBLE RUNTIME STATE
READY

Start the trace

Move line by line and connect the Java statement to its runtime effect.

No state change yet.
QUICK REVISION

Recall the Essential Ideas

01 javac compiles source into bytecode; java launches a JVM.
02 The JDK includes development tools and an execution runtime.
03 A public class name must match its source filename.
04 The standard entry point is public static void main(String[] args).
05 Compile, runtime and logic errors require different debugging approaches.
06 Bytecode portability still depends on a compatible platform JVM.
INTERVIEW QUESTIONS

Explain, Do Not Memorize

Open each answer only after speaking your own version aloud.

1. Why is Java called platform independent?

The compiler targets JVM bytecode rather than one operating system's machine code. The same class files can run where a compatible JVM and required libraries exist. The JVM implementation itself is platform specific.

2. What is the difference between JDK and JVM?

The JDK is the developer distribution containing the compiler, launcher and tools. The JVM is the execution engine that loads, verifies and runs bytecode while managing runtime resources.

3. Why is main static?

The launcher needs a known entry point before an application object exists. A static method belongs to the class, so it can be invoked without first calling a constructor.

4. Does bytecode run directly on the CPU?

Normally no. The JVM interprets bytecode or compiles selected methods into native machine instructions using the JIT compiler.

5. What does the bytecode verifier do?

It checks structural rules such as valid instruction forms, stack usage and compatible types before execution, strengthening the JVM safety boundary.

PROGRAMMING PROBLEMS

Apply the Concept Independently

Use the CodeBhavya online compiler. Test normal input, a boundary value and one invalid case where relevant.

PROBLEM 01

Personal greeting

Read or assign a learner name, course and target role; print them as three clear sentences.

Show guidance

Keep data in separate variables and use meaningful labels in the output.

PROBLEM 02

Bill summary

Store item name, quantity and unit price, then print the total amount.

Show guidance

Use parentheses around the multiplication inside a concatenated expression.

PROBLEM 03

Command-line reporter

Print the number of command-line arguments and, when available, the first argument.

Show guidance

Check args.length before accessing args[0].

PROBLEM 04

Error classifier

Create one compile error, one runtime exception and one logic error in separate copies; record how each appears.

Show guidance

Examples: incompatible type, invalid array index, and a wrong formula.

CONCEPT CHECK

Verify Your Understanding

Every answer shows the correct choice and the reason immediately.

Which tool converts Java source into bytecode?
  1. java
  2. javac
  3. jar
  4. javadoc

Answer: javac `javac` is the Java compiler.

Which file normally contains bytecode?
  1. .java
  2. .class
  3. .txt
  4. .jvm

Answer: .class Compilation writes class files.

Why can bytecode run on different operating systems?
  1. It is plain text
  2. Each system provides a compatible JVM
  3. CPUs all use bytecode
  4. The compiler runs remotely

Answer: Each system provides a compatible JVM The JVM maps portable bytecode to platform execution.

What must match a public top-level class?
  1. Package and method
  2. Filename and class name
  3. Variable and value
  4. JVM and source line

Answer: Filename and class name `Program` must be declared in Program.java.

What is the return type of standard main?
  1. int
  2. String
  3. void
  4. boolean

Answer: void Java main completes without returning a value to the caller.

A missing semicolon usually causes:
  1. Compile error
  2. Garbage collection
  3. Network error
  4. Logic-only error

Answer: Compile error Parsing fails before execution.

An invalid array index is normally:
  1. A comment
  2. A runtime exception
  3. A bytecode file
  4. A package

Answer: A runtime exception The program compiles but the access is checked during execution.

The JIT compiler mainly:
  1. Writes source comments
  2. Compiles hot bytecode to native code
  3. Creates database tables
  4. Replaces javac

Answer: Compiles hot bytecode to native code JIT optimization accelerates frequently executed code.

Correct: 0 of 0 attempted · 8 total

You can explain how Java reaches the machine

  • Java separates source compilation from managed runtime execution.
  • JDK tools create and inspect programs; the JVM executes verified bytecode.
  • Class and main-method structure form the launch contract.
  • Errors should be classified before they are corrected.
  • Portability is provided by bytecode plus platform-specific JVM implementations.