KTU OOP Java2024 Scheme • 60M ESE
MODULE 1/Section 1: Introduction to Java

Structure of a Simple Java Program, CLI Compilation, PATH & JDK vs JRE vs JVM

HIGH PRIORITY (Prep Guide)Official Syllabus~15 min study
Official KTU 2024 Syllabus Topics Covered Here
Structure of a simple java programJava programming environment and runtime environmentCommand-line compilation and execution conceptsJava compiler

1. Topic Introduction

Every Java application—from a three-line exam snippet to a banking backend—follows a strict structural skeleton and passes through two distinct phases: compilation (javac) and runtime execution (java). Understanding what each keyword in public static void main(String[] args) does and how JDK, JRE, and JVM relate guarantees easy 3-mark and 6-mark theory points.

2. Start From Zero: Kitchen, Appliances, and Chef (JDK vs JRE vs JVM)

Think of the JDK as a complete culinary studio: it contains recipes, knives, ovens, and a chef (compiler + debugger + runtime). If a friend only wants to heat and eat pre-cooked meals (already compiled .class files) without cooking from scratch, they only need the JRE (microwave + utensils). Inside that JRE is the JVM—the heating core that actually converts instructions into warm food.

Key Takeaway: JDK = JRE + Development Tools (javac, javadoc, javap). JRE = JVM + Core Class Libraries (java.lang, java.util).

3. Essential Exam Definitions

JDK (Java Development Kit)
Complete software package needed to develop, compile, debug, and run Java applications. Contains the JRE plus development tools (javac, javap, javadoc, jdb).
JRE (Java Runtime Environment)
Software package containing the JVM, standard class libraries, and supporting runtime files needed solely to execute compiled Java programs (contains java, not javac).
Java Compiler (javac)
Command-line tool in JDK that checks .java source files for syntax, semantic, and type errors and translates valid source into .class bytecode.
PATH vs CLASSPATH
PATH tells the OS shell where JDK executables (javac, java) reside; CLASSPATH tells javac and the JVM where user-defined .class files and third-party .jar libraries are located.

4. Detailed Technical Explanation & Syntax

Anatomy of a Simple Java Program

A standard Java source file follows an ordered top-to-bottom layout: optional package statement -> optional import statements -> class declaration -> fields, constructors, and methods. Execution of a standalone Java application always begins at main().

  • public: Access modifier making main visible to the JVM outside the class/package.
  • static: Allows the JVM to invoke main() directly using the class name before any object of HelloKTU is created.
  • void: Indicates main() returns no value back to the JVM.
  • main: Fixed method identifier searched for by the JVM launcher as the entry point.
  • String[] args (or String args[]): Array capturing command-line arguments passed as strings when starting the program.
  • System.out.println(...): System is a final class in java.lang; out is a public static final PrintStream field; println() prints text followed by a newline.
Java Syntax Template
// 1. Optional package statement
// 2. Optional import statements (java.lang is imported automatically)
public class HelloKTU {
public static void main(String[] args) {
System.out.println("Welcome to KTU OOP!");
}
}

Command-Line Compilation & Execution Workflow

When working on the command line (or behind the scenes in an IDE), two separate commands handle compile time and runtime:

  • Compile Time (javac HelloKTU.java): Note the .java file extension is REQUIRED. javac checks syntax and type safety. Any syntax violation (e.g., missing ; or mismatched types) aborts compilation and prints a compile-time error with line number. Success emits HelloKTU.class.
  • Runtime (java HelloKTU): Note you pass the CLASS NAME WITHOUT .class! The OS uses PATH to find java, which starts the JVM. The JVM uses CLASSPATH (default . current directory) to find HelloKTU.class, verifies bytecode, and calls HelloKTU.main().
  • Runtime Errors vs Compile-Time Errors: Syntax errors happen at compile time (javac). Logic failures like division by zero (10 / 0) compile cleanly and fail at runtime (ArithmeticException).

Programming Environment: Command Line vs IDE Concept

Students can write Java using a simple text editor + terminal (javac/java) or an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, or NetBeans. An IDE automates calling javac and java under the hood, manages CLASSPATH, and highlights syntax errors early—yet the underlying JDK compilation and JRE execution pipeline remains identical.

5. Working Principle: Command-Line Lifecycle From Source to Output

  1. Save source code in HelloKTU.java matching the public class HelloKTU name.
  2. Run javac HelloKTU.java — OS locates javac via PATH; compiler validates syntax and produces HelloKTU.class.
  3. Run java HelloKTU — OS launches java; JVM searches CLASSPATH (default .), loads HelloKTU.class, and verifies bytecode.
  4. JVM calls HelloKTU.main(String[] args) without constructing a HelloKTU object because main is static.

6. Examination-Ready Diagrams

Diagram 2

Java Program Compilation and Execution

Source CodeProgram.javaJava Compilerjavac Program.javaBytecode (.class)Platform Independent (WORA)Windows JVMInterpreter/JIT -> x64 MachineLinux JVMInterpreter/JIT -> ELF MachinemacOS JVMInterpreter/JIT -> ARM64/x64Compile Time (Once)Runtime (Anywhere)

Two-stage hybrid compilation and interpretation flow demonstrating Write Once, Run Anywhere (WORA) across Windows, Linux, and macOS.

Key Components
  • Source Code (.java): Human-readable Java class file written by the programmer.
  • Java Compiler (javac): Checks syntax/types and translates .java into platform-independent bytecode.
  • Bytecode (.class): Intermediate binary instructions executed by any JVM.
  • Platform-Specific JVM: Windows JVM, Linux JVM, or macOS JVM interprets/JIT-compiles bytecode into native machine code.
Step-by-Step Flow
  1. Write source file e.g. HelloWorld.java.
  2. Run javac HelloWorld.java to compile source into HelloWorld.class (Bytecode).
  3. Run java HelloWorld to launch the OS-specific JVM.
  4. JVM loads bytecode, verifies it, and translates it via Interpreter + JIT into host CPU machine code.
How to Draw in KTU Exam
  • Use a clean left-to-right pipeline: .java -> [javac Compiler] -> .class (Bytecode).
  • Branch .class (Bytecode) into 3 boxes: JVM (Windows), JVM (Linux), JVM (macOS) -> Machine Code.
  • Label .class clearly as 'Platform Independent Bytecode (WORA)'.

7. Complete Java Code Examples & Tracing

Complete Structure & Execution Trace of a Simple Java ProgramProgramStructureDemo.java
Shows comments, class block, static field, helper method call, and standard output in a valid standalone program.
1// Single-line comment: Structure of a simple Java program
2import java.lang.Math; // Optional since java.lang is auto-imported
3
4public class ProgramStructureDemo {
5 static String course = "KTU OOP Concepts";
6
7 static void printHeader() {
8 System.out.println("=== " + course + " ===");
9 }
10
11 public static void main(String[] args) {
12 printHeader();
13 int maxMarks = 60;
14 double passingScore = Math.ceil(maxMarks * 0.40);
15 System.out.println("ESE Max Marks: " + maxMarks);
16 System.out.println("Minimum 40% Reference: " + passingScore);
17 }
18}
Expected Output
=== KTU OOP Concepts ===
ESE Max Marks: 60
Minimum 40% Reference: 24.0
Keywords:importpublicclassstaticvoidmainString
Common Exam & Coding Mistakes
  • Running java ProgramStructureDemo.class instead of java ProgramStructureDemo (causes ClassNotFoundException).
  • Omitting static on main() — the program compiles, but JVM throws Main method is not static at runtime.
  • Writing Class with uppercase C instead of lowercase class.

Comparison Tables

JDK vs JRE vs JVM
CriteriaJDK (Java Development Kit)JRE (Java Runtime Environment)JVM (Java Virtual Machine)
Full FormJava Development KitJava Runtime EnvironmentJava Virtual Machine
RoleCompile, debug & run Java appsRun compiled Java apps onlyLoad, verify & execute bytecode
Physical ExistenceInstalled physically on diskInstalled physically on diskVirtual runtime engine inside JRE
Tools Includedjavac, java, javap, javadoc, JREjava, runtime libraries (no javac)No CLI tools; executes bytecode
RelationshipSuperset of JRESubset of JDK, Superset of JVMCore execution subset of JRE
PATH vs CLASSPATH
FeaturePATHCLASSPATH
Used ByOperating System shell (CMD/Bash)Java Compiler (javac) & JVM (java)
Points ToJDK bin folder (javac, java binaries)User .class root folders and .jar files
DefaultOS system/user binary foldersCurrent working directory (.)

Real-World Applications

  • Build pipelines and continuous integration servers invoke javac and java directly via CLI.
  • Servers running production Java apps only require a JRE/runtime image rather than full development tools.

Important KTU Exam Points

  • Memorize what every token in public static void main(String[] args) means — extremely common 3-mark question.
  • Always write JDK = JRE + Development Tools and JRE = JVM + Runtime Libraries.
  • Contrast javac File.java (requires .java) vs java ClassName (no .class).

Memory Techniques & Revision

  • PSVM: public (JVM can see it), static (no object needed), void (returns nothing), main (entry name).
  • Subset Nesting: JVM ⊂ JRE ⊂ JDK.

Common Pitfalls to Avoid

  • Confusing PATH (for OS finding javac.exe) with CLASSPATH (for JVM finding .class files).
  • Thinking void means main accepts no parameters — void is the RETURN type; (String[] args) is the parameter list.

KTU-Style Questions & Exam-Ready Answers

Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.

  • public: Allows the JVM (located outside the class/package) to access and invoke main.
  • static: Enables the JVM to call main directly via the class name without creating an object first.
  • void: Specifies that main returns no value to the JVM upon completion.
  • main(String[] args): Standard entry-point identifier where args receives command-line strings.

Quick Understanding Check

1. Which command correctly compiles and then runs a file containing public class Exam?

2. What happens if main is declared without the static keyword?