Skip to main content

Java

Illustrating Epsilon Closure in Java

Learn how to compute epsilon-closure in Java for an NFA. This post explains the concept, walks through the recursive algorithm with descriptive variable names, provides sample output, and includes a detailed explanation of the results.

Multithreading Example in Java

In this post, we implement a basic Multithreading example in Java. Multithreading allows multiple threads to execute concurrently within a single program, enabling tasks to run in parallel rather than one after another. Java has built-in support for multithreading through the Thread class and the Runnable interface. What is Multithreading? A thread is the smallest unit of execution within a process. When a program creates multiple threads, the operating system’s thread scheduler interleaves their execution — giving each thread a slice of CPU time in turn. This makes it appear as though they are running simultaneously (and on multi-core systems, they actually can be). In this example, we create two threads by extending the Thread class and overriding its run() method. Each thread prints a label 4 times, pausing 500 ms between prints. Both threads run concurrently, so their output interleaves. start() — Tells the JVM to create a new OS-level thread and invoke run() on it. Calling run() directly would execute it on the current thread, not a new one. Thread.sleep(ms) — Pauses the current thread for the given number of milliseconds, allowing other threads to execute. InterruptedException — Must be caught when calling sleep(). It fires if another thread interrupts this one while it is sleeping.

Implementing Tower of Hanoi Problem in Java

The Tower of Hanoi is a classic mathematical puzzle that elegantly demonstrates the power of recursion. It consists of three rods (pegs) and a number of disks of different sizes that can slide onto any rod. The puzzle begins with all disks stacked in ascending size order on one rod (smallest on top) and the goal is to move the entire stack to another rod. Three rules must be followed: Only one disk may be moved at a time. A disk may only be moved if it is the uppermost disk on its rod. No disk may be placed on top of a smaller disk. The minimum number of moves required to solve the puzzle with n disks is 2n − 1. For 3 disks, that's 7 moves; for 10 disks, 1023 moves.

Implementation of Stack in Java

A stack is a LIFO (Last In, First Out) abstract data type where all insertions and deletions happen at the same end, called the top. Think of a stack of plates: you always add to the top and remove from the top. Stacks are used everywhere in computing — from function call management to expression evaluation and undo functionality. In this post, we implement a stack in Java using a fixed-size integer array, with push, pop, peek, and display operations accessible through a menu-driven console program.

Implementing Singly Linked List in Java

A singly linked list is a linear data structure made up of nodes, where each node contains a data field and a pointer to the next node in the sequence. Unlike arrays, linked lists do not require contiguous memory — elements can be inserted or removed at any position without shifting other elements. In this post, we implement a full-featured singly linked list in Java that supports insertion (at first, at last, at a given position), deletion (from first, last, or a given position), search, and display. A singly linked list — each node stores a value and a link to the next node.

Java Implementation of Queue using Linked List

A queue is a FIFO (First In, First Out) data structure where elements are added at the rear and removed from the front. While a queue can be implemented using a fixed-size array, backing it with a linked list removes the capacity constraint — the queue grows and shrinks dynamically as elements are enqueued and dequeued. In this post, we implement a queue using a singly linked list in Java, supporting enqueue, dequeue, peek, and display operations.

Implementing Quick Sort in Java

Quick Sort is a highly efficient, comparison-based sorting algorithm that uses the divide-and-conquer strategy. It selects a pivot element, partitions the array into elements smaller than the pivot and elements larger than the pivot, then recursively sorts each partition. Its average-case time complexity is O(n log n), making it one of the fastest general-purpose sorting algorithms in practice.

Java Program to Convert Infix Notation to PostFix Notation

Infix notation is the standard way we write arithmetic expressions, with operators between operands — e.g., A + B. Postfix notation (also called Reverse Polish Notation or RPN) places operators after their operands — e.g., A B +. Postfix expressions can be evaluated by machines without needing parentheses or operator-precedence rules, making them ideal for expression evaluators and compilers. In this post, we implement a Java program that converts an infix expression to its equivalent postfix form using a character stack and an operator-precedence table.