Java Detailed Notes (Unit I)
1. The Java Language
Features of Java
Java is one of the most popular programming languages. It is widely used because of its unique features: -
Simple: Java is designed to be easy to understand, with fewer complexities compared to languages like C++
(no pointers, no operator overloading). - Object-Oriented: Everything in Java is represented as an object. It
models real-world problems using classes and objects. - Platform Independent: Java uses the concept of
Write Once, Run Anywhere (WORA). Code is compiled into bytecode, which runs on any system with a JVM. -
Secure: Java eliminates risks like explicit pointer handling and provides features like Bytecode Verifier and
Security Manager. - Robust: Java provides strong memory management and automatic garbage collection.
It also handles runtime errors using exceptions. - Multithreaded: Java supports execution of multiple tasks
simultaneously using threads. - High Performance: Although interpreted, Java uses Just-In-Time (JIT)
compilers to improve execution speed. - Distributed: Java supports distributed computing using RMI
(Remote Method Invocation) and CORBA. - Dynamic and Extensible: Java supports dynamic linking of
classes and can adapt to changing environments.
Java Programming Format
A Java program follows a specific structure: 1. Package declaration (optional) 2. Import statements
(optional) 3. Class declaration 4. Main method
class Example {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}
Java Tokens
Tokens are the smallest elements of a Java program. - Keywords: Reserved words (e.g., class, static, void). -
Identifiers: Names for variables, methods, classes. - Literals: Fixed values such as numbers (100),
characters ('A'), strings ("Hello"). - Operators: Symbols that perform operations (+, -, , /). - Separators*:
Symbols like ; , {} , () , [] .
Java Statements
Statements are instructions that a Java program executes. - Expression Statements: Assignments ( a =
10; ). - Control Statements: if-else, switch-case, loops. - Declaration Statements: Declaring variables and
objects.
1
Java Data Types
• Primitive Types: byte, short, int, long, float, double, char, boolean.
• Non-Primitive Types: Strings, Arrays, Classes, Interfaces.
Typecasting
• Widening (Implicit): Conversion from smaller to larger type.
• Narrowing (Explicit): Conversion from larger to smaller type.
Arrays
An array is a collection of similar type elements. - Single Dimensional Array: Stores values linearly. - Multi-
Dimensional Array: Stores values in a matrix form.
2. OOPS (Object-Oriented Programming System)
Class
A class is a blueprint that defines the data members and methods of objects.
Object
An object is an instance of a class that represents a real-world entity.
Static Keyword
• Static variables: Shared by all objects.
• Static methods: Belong to class, can be called without creating objects.
• Static blocks: Used for static initialization.
Constructors
Special methods used for initializing objects. They have the same name as the class.
this Keyword
Refers to the current instance of a class.
super Keyword
Refers to the immediate parent class object. It is used to call parent constructors and methods.
Inheritance
Process of acquiring properties and behavior of a parent class by a child class.
2
Polymorphism
• Compile-time (Overloading): Multiple methods with the same name but different parameter lists.
• Runtime (Overriding): A subclass provides a specific implementation of a method already defined in
the parent class.
Abstraction
Hiding implementation details and showing only the functionality. Achieved using abstract classes and
interfaces.
Encapsulation
Wrapping data and code together. Achieved by declaring variables as private and providing public getter
and setter methods.
Abstract Classes
A class declared with the abstract keyword. It may contain abstract methods (without a body).
Interfaces
A collection of abstract methods and constants. Supports multiple inheritance.
3. String Manipulations
String
Strings are immutable sequences of characters. Once created, they cannot be changed.
StringBuffer
• Mutable and synchronized.
• Used when frequent modifications are needed.
StringTokenizer
• Used to break a string into tokens based on a delimiter.
4. Packages
Predefined Packages
• [Link]: Provides fundamental classes (Object, String, Math).
• [Link]: Utility classes like Collections, Scanner, Date.
3
• [Link]: Input and output classes (File, BufferedReader).
• [Link]: For database operations.
• [Link]: GUI toolkit.
User Defined Packages
Developers can create custom packages to organize classes.
Access Specifiers
• public: Accessible everywhere.
• protected: Accessible within same package and subclasses.
• default: Accessible within the same package.
• private: Accessible only within the class.
5. Exception Handling
Introduction
Exception handling ensures normal flow of the program even after runtime errors.
Predefined Exceptions
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException
Try-Catch-Finally
• try: Code that may generate exception.
• catch: Handles the exception.
• finally: Executes always, regardless of exception.
Exception Handling Flow:
try → catch (if error occurs) → finally (always executed)
throw and throws
• throw: Used to explicitly throw an exception.
• throws: Used in method declaration to declare exceptions.
User Defined Exceptions
Programmer can create custom exceptions by extending Exception class.
4
6. Multithreading
Thread Creation
• By extending Thread class.
• By implementing Runnable interface.
Thread Life Cycle
1. New → 2. Runnable → 3. Running → 4. Waiting/Blocked → 5. Terminated
Thread Life Cycle Diagram:
New → start() → Runnable → run() → Running
↑ ↓
└────────────── Terminated ← Waiting/Blocked
Methods
• start(), run(), sleep(), join(), yield(), isAlive().
Synchronization
Prevents thread interference and data inconsistency.
wait(), notify(), notifyAll()
• wait(): Pauses thread execution.
• notify(): Wakes up a single waiting thread.
• notifyAll(): Wakes up all waiting threads.
7. Networking
Introduction
Networking in Java allows communication between two or more machines.
Socket
A socket is an endpoint for communication.
5
Server Socket
Used on the server side to accept client requests.
Client-Server Communication
• Server: Listens for client connections using ServerSocket.
• Client: Connects to server using Socket.
Client-Server Communication Flow:
Client -----> Request -----> ServerSocket (Server)
Client <----- Response <---- Socket Communication