Category: Java tutorial
-
Multi-Level Sorting using Comparator in Java
1. Introduction Sorting is one of the most common operations in programming, especially when working with collections of objects such as lists of employees, products, or students. In Java, sorting objects based on multiple fields (or multi-level sorting) is often required when one attribute alone is not sufficient to determine order. For example: This kind…
-
Java Memory Model (JMM)
1. Introduction Modern Java programs frequently use multithreading to improve performance, responsiveness, and scalability. However, with multiple threads accessing shared data, problems like inconsistent views of memory, race conditions, and visibility issues can arise. To manage these challenges, Java introduced the Java Memory Model (JMM) as part of the Java Language Specification (JLS) in Java…
-
Printing Even and Odd Numbers Using Two Synchronized Threads in Java
1. Problem Description We want: Both should print numbers in sequence, i.e.,1 2 3 4 5 6 7 8 … (not random interleaving). To achieve this, we’ll make the two threads communicate and coordinate using wait() and notify() on a shared object lock. 2. Core Idea 3. Step-by-Step Logic This ensures alternating execution. 4. Complete…
-
How ExecutorService manage threads?
1. Overview: What ExecutorService Really Is At a high level, ExecutorService is an abstraction built on top of thread pools.When you submit a task (a Runnable or Callable), the service does not create a new thread every time.Instead, it reuses a fixed set of worker threads that continuously fetch and execute tasks from a queue.…
-
Shallow Copy vs Deep Copy in Java
In Java, copying an object is a common operation, especially when working with mutable classes, collections, or when implementing features like cloning, caching, or undo mechanisms. However, not all copies are the same. The way objects are copied determines whether the new object shares the same references or gets completely independent copies of its data.…
-
LinkedHashMap
Introduction to LinkedHashMap LinkedHashMap is a class in Java’s Collections Framework that combines the power of a HashMap with a linked list to maintain the insertion order or access order of entries. It belongs to the package: and was introduced in Java 1.4. A LinkedHashMap is very useful when you want to store key-value pairs…
-
JEP 456: Unnamed Variables & Patterns
What is JEP 456? JEP 456 is a feature of Java 22 that finalizes the capability to use “unnamed variables” and “unnamed patterns”. In short, when you need a variable binder (in a local variable declaration, exception parameter, lambda parameter, loop header, or pattern matching) but you are never going to use that variable, you…
-
XOR in Java
1. Introduction XOR stands for Exclusive OR. It is a bitwise operator used to compare two bits. In Java, XOR is represented by the ^ operator. XOR Truth Table A B A ^ B 0 0 0 0 1 1 1 0 1 1 1 0 Key Properties: These properties make XOR extremely useful in…
-
How G1 Garbage Collector (G1GC) Works
The G1 Garbage Collector (G1GC) is a modern, server-style garbage collector in Java, introduced in Java 7 (experimental) and fully supported from Java 8 onwards. G1GC is designed for large heaps (multiple gigabytes) and applications where low pause times are important, such as high-throughput server applications or real-time systems. It differs from traditional collectors (like…
-
Collectors.groupingBy
Collectors.groupingBy is a powerful collector in Java Streams used to group elements of a stream based on a classifier function. It is commonly used for aggregation tasks like counting, summing, or finding max/min per group. 1. Basic Concept Collectors.groupingBy groups stream elements based on a classifier function: 2. Example 1: Group Strings by Length Output:…
