IT 234

Object Oriented Programming with Java

TU BIM · Semester 2 · BIM curriculum effective from 2021

Requirement
required
Credits
3
Study Guide
7 units available

Chapter notes

Authoritative study guide across 7 syllabus units

Open a unit to read its study notes.

Unit 1: Introduction· 4 LHs· ~5 marks

Pass path: Learn the 4 OOP principles, JDK vs JRE vs JVM, and writing basic Java console programs in Part 1 and Part 2. Top path: Master the JVM internal subsystem architecture (ClassLoader, JIT compiler, garbage collection) in Part 2 and Part 4.

Part 1: Core Concepts (2 Marks)

  • ★ Bytecode and WORA: An intermediate, architecture-neutral instruction set generated by the javac compiler (.class file) that cannot execute directly on hardware, but is interpreted or JIT-compiled by the Java Virtual Machine (JVM) on any operating system, realizing “Write Once, Run Anywhere” (WORA).
  • ★ JDK vs. JRE vs. JVM:
    • JVM (Java Virtual Machine): The abstract computing runtime that executes compiled bytecode on physical hardware.
    • JRE (Java Runtime Environment): JVM + Core Java Class Libraries (rt.jar) required to run compiled Java programs.
    • JDK (Java Development Kit): JRE + Development Tools (javac compiler, javadoc, jdb debugger) required to author Java software.
  • ★ Four Pillars of OOP:
    1. Encapsulation: Binding data variables and manipulating methods into a single unit (class), hiding internal state (private).
    2. Abstraction: Exposing only essential interface features while concealing internal implementation complexities.
    3. Inheritance: Deriving new classes from existing classes, promoting hierarchical code reuse (extends).
    4. Polymorphism: The ability of a single method call or interface to manifest different behaviors based on the runtime object.

Part 2: Structural Frameworks (10 Marks)

1. The Java Virtual Machine (JVM) Internal Architecture

JVM Runtime Architecture

JVM Architecture
JVM Architecture

When evaluating the internal execution pipeline of the JVM:

  1. ClassLoader Subsystem:
    • Dynamically loads .class bytecode files into memory.
    • Performs three sequential phases: Loading (Bootstrap, Extension/Platform, Application ClassLoader), Linking (Verification of bytecode safety, Preparation of static memory, Resolution of symbolic references), and Initialization (executing static blocks).
  2. Runtime Data Areas (JVM Memory Segments):
    • Method Area: Shared memory storing class-level structures, method code, and static variables.
    • Heap Area: Shared runtime memory where all dynamic Java objects (new) and arrays are allocated; managed by the Garbage Collector.
    • JVM Stack: Thread-private memory allocating a stack frame for every method invocation, holding local variables and partial results.
    • PC Registers: Holds the address of the currently executing JVM instruction per thread.
    • Native Method Stack: Facilitates execution of C/C++ native system routines (JNI).
  3. Execution Engine:
    • Interpreter: Reads and executes bytecode instructions sequentially.
    • Just-In-Time (JIT) Compiler: Analyzes frequently executed “hot spot” bytecode and compiles it directly into native CPU machine code, boosting runtime speed.
    • Garbage Collector (GC): Automatically reclaims heap memory occupied by unreferenced objects.

Part 3: Distinction Tables

Primary Java Software Bundles: JDK vs. JRE

Feature JDK (Java Development Kit) JRE (Java Runtime Environment)
Primary Audience Software engineers, Java developers General computer end-users
Core Components JRE + Development tools (javac, jar, javap) JVM + Core standard Java libraries
Code Execution Compiles .java source code into .class bytecode Executes existing pre-compiled .class bytecode
Installation Requirement Mandatory on programmer workstations Installed on client machines running Java apps

Part 4: Worked Problems & Exam Traps

Classic Java Console I/O Program

import java.util.Scanner;

public class StudentInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter student name: ");
        String name = scanner.nextLine();

        System.out.print("Enter roll number: ");
        int roll = scanner.nextInt();

        System.out.print("Enter GPA: ");
        double gpa = scanner.nextDouble();

        System.out.println("\n--- Academic Summary ---");
        System.out.println("Name: " + name + " | Roll: " + roll + " | GPA: " + gpa);
        scanner.close();
    }
}

Students lose marks here because they name the Java source file differently from the public class declared inside it (in Java, a file declaring public class StudentInput must strictly be saved as StudentInput.java).