Object Oriented Programming using JAVA
Comprehensive exam revision notes for BCA-201N Unit 1 (Java Fundamentals, JVM Architecture & Language Syntax). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Object Oriented Programming using JAVA
Java Fundamentals, JVM Architecture & Language Syntax
Core Definitions & Key Concepts
What is the primary difference between JDK, JRE, and JVM
- JVM (Java Virtual Machine): An abstract execution engine that interprets/JIT-compiles Java bytecode into native machine instructions.
- JRE (Java Runtime Environment): JVM + core class libraries (rt.jar); provides environment to run compiled Java programs.
- JDK (Java Development Kit): JRE + developer tools (javac compiler, javadoc, jar, debugger); required for writing and compiling Java code.
Why is Java termed 'Platform Independent'
Java follows the WORA (Write Once, Run Anywhere) paradigm. The Java compiler (javac) compiles source code into intermediate, architecture-neutral bytecode (.class files). Any computer with a platform-specific JVM can execute this bytecode without modifying or recompiling the original source code.
What is the Just-In-Time (JIT) Compiler
The JIT compiler is a component of the JVM execution engine that compiles frequently executed bytecode sequences ('hot spots') directly into native hardware machine code at runtime, significantly boosting execution speed over pure interpretation.
Java's memory layout: Stack vs Heap
- Stack Memory: Allocates execution frames for method invocations, local primitive variables, and references to objects. Memory allocation/deallocation is LIFO and handled automatically.
- Heap Memory: Global pool where all Java objects and arrays reside. Managed dynamically by the Garbage Collector.
What are the Primitive Data Types in Java and their sizes
byte(1 byte, 8-bit signed)short(2 bytes, 16-bit signed)int(4 bytes, 32-bit signed)long(8 bytes, 64-bit signed)float(4 bytes, IEEE 754)double(8 bytes, IEEE 754)char(2 bytes, 16-bit Unicode UTF-16)boolean(logical true/false)
Widening (Implicit) and Narrowing (Explicit) Type Casting
- Widening: Converting a smaller data type to a larger type (e.g.,
inttodouble). Done automatically by compiler with zero data loss. - Narrowing: Converting a larger type to a smaller type (e.g.,
doubletoint). Requires explicit cast(int)dand can lead to truncation and precision loss.
Why does Java use Unicode instead of ASCII for character representation
ASCII only supports 128 characters (English alphabet and controls). Java uses 16-bit Unicode (UTF-16), allowing it to natively represent international alphabets, symbols, and scripts (Hindi, Japanese, Chinese, Arabic) supporting global multilingual software.
What is the role of public static void main(String[] args)
public: Accessible to the JVM from outside the class package.static: Can be invoked by the JVM without instantiating an object of the enclosing class.void: Does not return any value to the JVM process.String[] args: Accepts command-line arguments as an array of String tokens.
What is Garbage Collection in Java
Automatic memory reclamation performed by the JVM daemon thread. It identifies and deletes objects on the heap that are no longer reachable by any active references, preventing memory leaks without manual pointers (free() or delete).
The difference between System.out.print() and System.out.println()
System.out.print() outputs text to the standard console without advancing to a new line. System.out.println() prints the text and automatically appends a platform-specific newline character (\n), positioning the cursor at the beginning of the next line.
Conceptual & Architectural Analysis
The Architecture and Internal Subsystems of the Java Virtual Machine (JVM)
The JVM consists of three primary internal subsystems:
- Class Loader Subsystem: Responsible for Loading (locates
.classfiles via Bootstrap, Extension, and Application ClassLoaders), Linking (Verification of bytecode safety, Preparation of static fields, and Resolution of symbolic references), and Initialization (executing static initializers). - Runtime Data Areas:
- Method Area: Stores class structure, method code, and constant pool.
- Heap Area: Stores all runtime objects and array instances.
- JVM Stacks: Thread-private frames containing local variables and operand stacks.
- PC Registers: Tracks instruction address currently being executed.
- Native Method Stacks: Holds C/C++ native method execution frames.
- Execution Engine: Contains Bytecode Interpreter, Just-In-Time (JIT) Compiler, and Garbage Collector.
Compare Java with C and C++ across Core Paradigms
| Feature | C | C++ | Java |
|---|---|---|---|
| Paradigm | Pure Procedural | Multi-paradigm (Procedural + OOP) | Pure Object-Oriented |
| Platform Dependency | Platform Dependent (compiled to native) | Platform Dependent (compiled to native) | Platform Independent (Bytecode + JVM) |
| Pointers | Direct raw pointers | Raw pointers + references | No explicit pointers (safe references only) |
| Memory Management | Manual (malloc/free) | Manual (new/delete) | Automatic Garbage Collection |
| Multiple Inheritance | Not supported | Supported for classes | Supported only via Interfaces |
| Operator Overloading | No | Yes | No (except + for Strings) |
| Runtime Model | Direct execution | Direct execution | JRE / JVM execution |
The Java Compilation and Execution Process with Diagrammatic Flow
The Java pipeline combines compilation and interpretation in a two-stage process:
+--------------------+ javac +----------------------+
| Hello.java | -----------------> | Hello.class |
| (Java Source Code) | Compiler | (Platform Bytecode) |
+--------------------+ +----------------------+
|
+-------------------------------------+-------------------------------------+
| | |
v v v
+------------------+ +------------------+ +------------------+
| Windows JVM | | Linux JVM | | macOS JVM |
+------------------+ +------------------+ +------------------+
| | |
v v v
[ Windows Native ] [ Linux Native ] [ macOS Native ]
[ Machine Code ] [ Machine Code ] [ Machine Code ]javac Hello.javaparses syntax and emits architecture-neutral bytecode (Hello.class).java Helloloads bytecode into JVM; ClassLoader verifies bytecode; Execution Engine interprets/JIT-compiles into native instructions.
Java Operators and Operator Precedence Hierarchy
Java operators are classified into:
- Unary:
++,--,+,-,!,~ - Arithmetic:
*,/,%(higher precedence) followed by+,- - Relational & Equality:
<,<=,>,>=,instanceof,==,!= - Bitwise & Shift:
&,^,|,<<,>>,>>>(unsigned right shift) - Logical: Short-circuit
&&,|| - Ternary:
condition ? expr1 : expr2 - Assignment:
=,+=,-=,*=,/=,%=
Short-circuit evaluation: In A && B, if A evaluates to false, B is not evaluated. In A || B, if A evaluates to true, B is skipped.
Control Flow Statements in Java with code examples
Java provides standard structured control flow:
- Decision Making:
if-elseandswitch-case(supportsint,char,enum, andStringsince Java 7). - Looping Statements:
forloop: Traditional counter-controlled.- Enhanced
for-eachloop:for (Type var : collection)for array and iterable traversal. whileloop: Entry-controlled condition.do-whileloop: Exit-controlled, guarantees at least one execution.- Jump Statements:
break(terminates loop/switch; supports labeled break for nested loops),continue(skips to next iteration), andreturn.
In-Depth Analysis & Implementation
Design and Implement a Complete Java Program Demonstrating JVM Execution, Command-Line Arguments, Data Types, and Formatted I/O
Here is a complete, production-grade Java application demonstrating these core concepts:
/**
* University Examination Model Solution: BCA-201N Unit 1
* Topic: Fundamentals, Command-Line Arguments, and Type Casting
*/
public class JavaFundamentalsDemo {
public static void main(String[] args) {
System.out.println("====================================================");
System.out.println(" MJPRU BCA SEMESTER 3 - JAVA FUNDAMENTALS DEMO ");
System.out.println("====================================================");
// 1. Command-Line Arguments Demonstration
if (args.length > 0) {
System.out.println("Command-line arguments supplied:");
for (int i = 0; i < args.length; i++) {
System.out.printf(" Arg[%d]: %s%n", i, args[i]);
}
} else {
System.out.println("No command-line arguments passed. Using default student data.");
}
// 2. Primitive Data Types and Variable Initialization
byte studentAge = 20;
short collegeCode = 1045;
int rollNumber = 2400512;
long enrollmentId = 202409015523L;
float attendancePercentage = 88.75f;
double semesterGpa = 8.94;
char grade = 'A';
boolean isFeePaid = true;
// 3. Widening and Narrowing Type Casting
int implicitWidened = studentAge; // byte -> int (Widening)
int explicitNarrowed = (int) attendancePercentage; // float -> int (Narrowing, truncates decimal)
// 4. Formatted Output
System.out.println("\n--- Student Academic Profile ---");
System.out.printf("Roll Number : %d%n", rollNumber);
System.out.printf("Enrollment ID : %d%n", enrollmentId);
System.out.printf("College Code : %d%n", collegeCode);
System.out.printf("Age : %d years (Widened to int: %d)%n", studentAge, implicitWidened);
System.out.printf("Attendance : %.2f%% (Truncated: %d%%)%n", attendancePercentage, explicitNarrowed);
System.out.printf("Semester GPA : %.2f%n", semesterGpa);
System.out.printf("Final Grade : %c%n", grade);
System.out.printf("Fee Clearance : %b%n", isFeePaid);
System.out.println("====================================================");
}
}Key Discussion Points:
- Demonstrates
String[] argsparsing with bounds safety. - Contrasts widening type promotion against explicit cast truncation.
- Uses
System.out.printfwith format specifiers%d,%f,%s,%c,%bfor clean terminal rendering.
Write a Detailed Technical Treatise on Garbage Collection Algorithms in Java (Mark-Sweep, Generational Garbage Collection, Heap Generations)
1. The Need for Automatic Memory Management
In languages like C++, programmers manually allocate (new) and deallocate (delete) memory. Forgetting deallocation causes memory leaks, while premature deallocation produces dangling pointers. Java eliminates both by introducing a runtime Garbage Collector (GC).
2. Generational Hypothesis
Empirical research across millions of enterprise applications established that most objects have very short lifespans (e.g., local variables inside a method frame). Consequently, the Java HotSpot JVM partitions the Heap into distinct generational zones:
+-------------------------------------------------------------------------+
| JAVA HEAP |
| +---------------------------------+ +-------------------------------+ |
| | YOUNG GENERATION | | OLD GENERATION | |
| | +-----------+ +----+ +----+ | | | |
| | | Eden | | S0 | | S1 | | | Tenured Space | |
| | | Space | |Surv| |Surv| | | | |
| | +-----------+ +----+ +----+ | | | |
| +---------------------------------+ +-------------------------------+ |
+-------------------------------------------------------------------------+3. Heap Generation Mechanics
- Eden Space (Young Gen): All newly instantiated objects (
new Object()) are initially placed here. When Eden fills up, a lightweight Minor GC triggers. - Survivor Spaces (S0 & S1): Live objects surviving an Eden GC are copied to S0. In the next Minor GC, surviving objects from Eden and S0 are moved to S1, and S0 is wiped clean. Active survivors alternate between S0 and S1.
- Tenured / Old Generation: Each surviving object tracks its GC 'age' (threshold typically 15). Once an object survives enough cycles, it is promoted to the Tenured Space. When Tenured fills, a heavyweight Major GC / Full GC executes.
4. Mark-and-Sweep Algorithm Phases
- Phase 1: Mark: The GC traverses the object graph starting from 'GC Roots' (local variables on active stacks, static fields, JNI pointers). All reachable objects are marked as alive.
- Phase 2: Sweep: The GC scans the heap memory and reclaims memory occupied by all unmarked (unreachable) objects.
- Phase 3: Compact: Surviving objects are shifted to the beginning of the memory space, leaving a single large contiguous free block to eliminate fragmentation.
The Object-Oriented Programming (OOP) Paradigm and How Java Implements the 4 Fundamental Pillars
The Object-Oriented paradigm structures software around real-world data entities (objects) rather than procedural functions. Java implements the 4 foundational pillars:
1. Encapsulation
Binding data variables and methods that operate on that data into a single unit (class), while hiding internal state via access specifiers (private). Access is provided strictly through public getters and setters.
2. Abstraction
Hiding internal implementation complexity while presenting only essential interface features to the user. Implemented in Java via Abstract Classes (abstract class Shape) and Interfaces (interface PaymentGateway).
3. Inheritance
The mechanism by which one class (subclass/child) inherits fields and methods of another class (superclass/parent) using the extends keyword, fostering code reusability and establishing 'is-a' relationships.
4. Polymorphism
The ability of an entity (method, object) to take on multiple forms. Divided into:
- Compile-Time (Static) Polymorphism: Method Overloading (same method name, different parameter signatures within the same class).
- Runtime (Dynamic) Polymorphism: Method Overriding (subclass provides specific implementation of a superclass method, dynamically dispatched at runtime via virtual method tables).
Important Exam Points & Quick Revision
Focus on the JVM internal block diagram, the distinction between JDK/JRE/JVM, and memory layout (Stack vs Heap). Be prepared to write clean code illustrating command-line argument processing and explicit type casting.
100% left on this page — finishing it closes the gap for Java Fundamentals, JVM Architecture & Language Syntax.
Java Fundamentals, JVM Architecture & Language Syntax still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.