Java Programming: Short Notes
Prepared for Semester Exam
December 13, 2025
1. Interthread Communication
Interthread Communication in Java allows multiple threads to communicate with each other
using shared resources. It ensures proper synchronization and coordination between threads,
preventing issues like data inconsistency.
Key Concepts:
• wait(): Suspends the thread until another thread invokes notify() or notifyAll().
• notify(): Wakes up one waiting thread.
• notifyAll(): Wakes up all waiting threads.
Example:
class Shared {
synchronized void produce() throws InterruptedException {
[Link] . println (”Producer is producing ... ”) ;
wait () ;
[Link] . println (”Producer resumed”);
}
synchronized void consume() {
[Link] . println (”Consumer consumed and notified”) ;
notify () ;
}
}
Applications: Producer-Consumer problems, coordination of threads in multithreaded
applications.
1
2. Deadlock
Deadlock occurs when two or more threads are waiting indefinitely for resources held by each
other. No thread can proceed, resulting in program halt.
Conditions for Deadlock:
1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait
Example:
synchronized(obj1) {
synchronized(obj2) {
// Thread 1
}
}
synchronized(obj2) {
synchronized(obj1) {
// Thread 2
}
}
Prevention Techniques: Resource ordering, avoiding nested locks, using tryLock with
timeout.
3. Executors Framework and Callable
Executors Framework in Java provides thread pool management for efficient thread execu-
tion. It avoids creating too many threads and reuses existing ones.
• ExecutorService: Core interface for managing thread pools.
• Callable: Similar to Runnable but can return a result and throw checked exceptions.
• Future: Holds the result of a Callable.
Example:
2
ExecutorService executor = Executors .newFixedThreadPool(3);
Callable <Integer> task = () −> 2 + 3;
Future<Integer> result = executor .submit( task ) ;
[Link] . println ( result . get () ) ;
executor . shutdown();
4. Iterators
Iterator is an interface used to traverse collections sequentially without exposing the underlying
structure.
Methods:
• hasNext(): Checks if more elements exist.
• next(): Returns the next element.
• remove(): Removes the current element.
Example:
List <String> list = new ArrayList<>();
Iterator <String> it = list . iterator () ;
while( it . hasNext() ) {
[Link] . println ( it . next () ) ;
}
Applications: Iterating lists, sets, or any Collection object safely.
5. Enhanced For-Loop
The Enhanced For-Loop simplifies iteration over arrays and collections without using indices.
Syntax:
for (Type element : collection ) {
// use element
}
Example:
int [] numbers = {1, 2, 3};
for ( int num : numbers) {
[Link] . println (num);
}
Benefits: Cleaner code, reduces errors, and avoids explicit iterators or index variables.
3
6. Sorting and Searching Collections
Sorting arranges elements in ascending or descending order, and searching locates an element.
Java Methods:
• [Link](list): Sorts a list using natural order.
• [Link](): Sorts in reverse.
• [Link](list, key): Searches a sorted list efficiently.
Example:
List <Integer> list = Arrays . asList (5, 2, 8) ;
Collections . sort ( list ) ;
int index = Collections . binarySearch ( list , 5) ;
Applications: Efficient data organization and retrieval in applications.
7. Abstract Class
An abstract class cannot be instantiated directly and may contain abstract methods (without
body) and concrete methods.
• Used to define common behavior for subclasses.
• Provides partial implementation.
• Supports polymorphism.
Example:
abstract class Shape {
abstract void draw() ;
void display () { [Link] . println (”Shape”); }
}
class Circle extends Shape {
void draw() { [Link] . println (” Circle drawn”); }
}
Applications: Base classes for geometric shapes, animals, or GUI components.
4
8. Dictionary
Dictionary is a legacy class in Java representing a key-value pair mapping. Each key is unique,
and values are retrieved using the key.
Example:
Dictionary <Integer , String > dict = new Hashtable<>();
dict . put (1, ”Java”) ;
[Link] . println ( dict . get (1) ) ;
Note: HashMap and Hashtable have mostly replaced Dictionary.
9. JDBC
JDBC allows Java programs to connect and interact with databases using SQL.
Steps:
1. Load driver class
2. Establish connection
3. Create Statement
4. Execute query/update
5. Process ResultSet
6. Close connection
Example:
Connection con = [Link]( url , user , pass) ;
Statement st = con. createStatement () ;
ResultSet rs = st .executeQuery(”SELECT ∗ FROM students”);
Applications: Database-driven applications like banking, inventory, and web apps.
10. Lambda Expressions
Lambda expressions provide a concise way to represent anonymous functions in Java.
Syntax:
( parameters ) −> expression
Example:
5
List <Integer> list = Arrays . asList (1,2,3) ;
list . forEach(n −> [Link]. println (n)) ;
Benefits: Cleaner code, functional programming style, used in streams, collections, and
event handling.
11. Final Keyword
The final keyword is used to define constants, prevent method overriding, or prevent inheritance.
• final variable: Cannot be reassigned
• final method: Cannot be overridden
• final class: Cannot be subclassed
Example:
final int x = 10;
final class A {}
final void show() {}
12. Socket Programming
Socket Programming enables communication between two machines over a network using
TCP or UDP.
• Socket: Client-side communication
• ServerSocket: Server-side listener
Example (TCP Server):
ServerSocket server = new ServerSocket(5000);
Socket client = server . accept () ;
BufferedReader in = new BufferedReade(new
InputStreamReader( client . getInputStream () ) ) ;
Applications: Networked applications like chat systems, HTTP servers, and multiplayer
games.