Unit 3: Java Programming
Chapter 1: Introduction to Java and OOP Principles
1. Origin and Evolution of Java
- Created by James Gosling at Sun Microsystems (later acquired by Oracle Corporation) in 1995.
- Originally named Oak (aimed at embedded smart consumer appliances), then renamed to Java.
- Key design motto: "Write Once, Run Anywhere" (WORA).
2. Key Features of Java
- Simple and Familiar: Syntax derived from C/C++, but removed complex, error-prone concepts like explicit pointers, memory deallocation, and multiple inheritance of classes.
- Platform Independent & Portable:
- Java programs are not compiled into machine-specific native code.
- They are compiled into platform-neutral Bytecode (
.class file).
- Any machine with a Java Virtual Machine (JVM) can execute this Bytecode.
- Object-Oriented: Almost everything in Java is an Object modeled from a Class.
- Robust & Secure:
- Strict compile-time and runtime type checking.
- Automatic Garbage Collection eliminates memory leaks.
- Absence of pointer arithmetic prevents unauthorized memory access.
- Multithreaded: Built-in support for executing concurrent threads to optimize CPU usage.
3. Java Execution Architecture: JDK, JRE, and JVM
+-----------------------------------------------------------------------------+
| JDK |
| (Java Development Kit: javac, jar, javadoc, debugger, development tools) |
| |
| +-----------------------------------------------------------------------+ |
| | JRE | |
| | (Java Runtime Environment: Java Class Libraries + Core Files) | |
| | | |
| | +-----------------------------------------------------------------+ | |
| | | JVM | | |
| | | (Java Virtual Machine: ClassLoader, Bytecode Verifier, JIT) | | |
| | +-----------------------------------------------------------------+ | |
| +-----------------------------------------------------------------------+ |
+-----------------------------------------------------------------------------+
1. JVM (Java Virtual Machine)
- An abstract computing machine that provides the runtime execution environment for Java Bytecode.
- The JVM is platform-dependent (different for Windows, Linux, macOS), but the Bytecode is platform-independent.
2. JRE (Java Runtime Environment)
- Provides the libraries, JVM, and other components required to run Java applications.
- Does not contain compilers or development tools.
3. JDK (Java Development Kit)
- The complete software package needed to develop, compile, and run Java programs.
- Contains JRE + Development Tools (
javac compiler, jar packager, javadoc, debugger).
4. Fundamental OOP (Object-Oriented Programming) Principles
1. Class and Object
- Class: A user-defined blueprint or template from which individual objects are created. Defines attributes (variables) and behaviors (methods).
- Object: A concrete instance of a class occupying physical memory.
// Class definition
class Student {
int rollNo; // Attribute / State
String name;
void display() { // Behavior / Method
System.out.println(rollNo + ": " + name);
}
}
// Creating an object
Student s1 = new Student();
2. Encapsulation
- Wrapping data (variables) and code (methods) together into a single unit (Class).
- Data hiding is achieved by making variables
private and exposing public getter/setter methods.
3. Data Abstraction
- Displaying only essential features to the user while hiding internal implementation complexities.
- Achieved using Abstract classes and Interfaces.
4. Inheritance
- The mechanism by which a new class (Subclass / Child) acquires properties and methods of an existing class (Superclass / Parent).
- Achieved using the
extends keyword. Enhances code reusability.
5. Polymorphism
- The ability of a message or method to take multiple forms.
- Compile-time Polymorphism (Static Binding): Method Overloading (multiple methods with same name but different parameter signatures).
- Runtime Polymorphism (Dynamic Binding): Method Overriding (subclass provides specific implementation of parent method).
5. Board Exam Focus Questions
- Explain the significance of Java Bytecode in achieving platform independence.
- Differentiate between JDK and JRE with a neat block diagram.
- Define Encapsulation and Data Abstraction. How are they related?
- What does the acronym WORA stand for? Explain how Java implements it.