Introduction to Java
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle) in 1995. It follows the principle of "Write Once, Run Anywhere" (WORA), meaning compiled Java code can run on all platforms without recompilation.
History of Java
- 1991: Project Oak started by James Gosling
- 1995: Java 1.0 released publicly
- 2006: Java made open source
- 2010: Oracle acquired Sun Microsystems
- 2014: Java 8 released with lambda expressions
- 2018: Java 11 released as LTS version
First Java Program
Java Features
Java has several distinctive features that make it popular for various applications.
Key Features of Java
- Platform Independent: Write Once, Run Anywhere (WORA)
- Object-Oriented: Based on objects and classes
- Simple: Easy to learn and use
- Secure: Provides security features
- Robust: Strong memory management and exception handling
- Multithreaded: Supports multiple threads of execution
- Architecture Neutral: Not tied to specific hardware architecture
- Dynamic: Supports dynamic loading of classes
Java Virtual Machine (JVM) Architecture
JVM is the heart of Java programming language that provides platform independence.
JVM Components
- ClassLoader: Loads class files
- Method Area: Stores class structure
- Heap: Runtime data area for objects
- Stack: Stores frames for method calls
- PC Registers: Stores address of current instruction
- Native Method Stack: Supports native methods
- Execution Engine: Executes bytecode
Java Execution Process
- Java source code (.java) is compiled to bytecode (.class)
- ClassLoader loads the bytecode into JVM
- Bytecode verifier checks code validity
- Interpreter/JIT compiler converts bytecode to machine code
- Execution engine runs the machine code
Data Types and Variables
Java is a strongly typed language, meaning every variable must be declared with a data type.
Primitive Data Types
| Data Type | Size | Range | Default Value |
|---|---|---|---|
| byte | 1 byte | -128 to 127 | 0 |
| short | 2 bytes | -32,768 to 32,767 | 0 |
| int | 4 bytes | -2^31 to 2^31-1 | 0 |
| long | 8 bytes | -2^63 to 2^63-1 | 0L |
| float | 4 bytes | ±3.4e^38 | 0.0f |
| double | 8 bytes | ±1.7e^308 | 0.0d |
| char | 2 bytes | 0 to 65,535 | '\u0000' |
| boolean | 1 bit | true/false | false |
Variable Declaration and Initialization
Operators in Java
Operators are special symbols that perform operations on operands.
Types of Operators
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, >, <,>=, <=
- Logical Operators: &&, ||, !
- Assignment Operators: =, +=, -=, *=, /=
- Unary Operators: ++, --
- Bitwise Operators: &, |, ^, ~, <<,>>, >>>
- Ternary Operator: ? :
Control Statements
Control statements determine the flow of execution in a Java program.
Decision Making Statements
- if statement: Executes block if condition is true
- if-else statement: Executes one block if true, another if false
- switch statement: Selects one of many code blocks to execute
Looping Statements
- for loop: Executes block for specific number of times
- while loop: Executes block while condition is true
- do-while loop: Executes block at least once, then repeats while condition is true
Object-Oriented Programming (OOP) Concepts
Java is a pure object-oriented programming language that supports all OOP principles.
Four Pillars of OOP
- Encapsulation: Wrapping data and methods into a single unit
- Inheritance: Creating new classes from existing ones
- Polymorphism: Ability to take many forms
- Abstraction: Hiding implementation details
Class and Object
A class is a blueprint for objects, and an object is an instance of a class.
Inheritance
Inheritance allows a class to inherit properties and behaviors from another class.
Types of Inheritance
- Single Inheritance: A class inherits from one base class
- Multilevel Inheritance: A class inherits from a derived class
- Hierarchical Inheritance: Multiple classes inherit from a single base class
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common super class.
Types of Polymorphism
- Compile-time Polymorphism (Static): Achieved through method overloading
- Runtime Polymorphism (Dynamic): Achieved through method overriding
Abstraction
Abstraction hides implementation details and shows only essential features.
Abstract Classes
- Cannot be instantiated
- Can have abstract and concrete methods
- Subclasses must implement abstract methods
Interfaces
- All methods are abstract by default (before Java 8)
- Supports multiple inheritance
- Can have default and static methods (Java 8+)
Encapsulation
Encapsulation is the technique of making fields private and providing public methods to access them.
Access Modifiers
| Modifier | Same Class | Same Package | Subclass | Other Packages |
|---|---|---|---|---|
| private | Yes | No | No | No |
| default | Yes | Yes | No | No |
| protected | Yes | Yes | Yes | No |
| public | Yes | Yes | Yes | Yes |
Exception Handling
Exception handling allows programs to handle runtime errors gracefully.
Exception Hierarchy
- Throwable: Superclass of all errors and exceptions
- Error: Serious problems that applications should not try to catch
- Exception: Problems that can be caught and handled
- RuntimeException: Unchecked exceptions
- Other Exceptions: Checked exceptions
Exception Handling Keywords
- try: Block of code to test for errors
- catch: Block of code to handle the error
- finally: Block of code that always executes
- throw: Throws an exception explicitly
- throws: Declares exceptions that a method might throw
Multithreading
Multithreading allows concurrent execution of two or more threads for maximum utilization of CPU.
Thread Creation Methods
- Extending Thread class
- Implementing Runnable interface
Thread States
- New
- Runnable
- Running
- Blocked/Waiting
- Terminated
Collections Framework
Collections framework provides architecture to store and manipulate groups of objects.
Main Collection Interfaces
- List: Ordered collection, allows duplicates
- Set: Unordered collection, no duplicates
- Queue: FIFO ordering
- Map: Key-value pairs, no duplicate keys
Common Collection Classes
| Interface | Implementation Classes | Characteristics |
|---|---|---|
| List | ArrayList, LinkedList, Vector | Ordered, indexed, allows duplicates |
| Set | HashSet, LinkedHashSet, TreeSet | No duplicates, unordered (except LinkedHashSet) |
| Queue | PriorityQueue, LinkedList | FIFO, priority-based ordering |
| Map | HashMap, LinkedHashMap, TreeMap, Hashtable | Key-value pairs, no duplicate keys |