I'm always excited to take on new projects and collaborate with innovative minds.
contact@niteshsynergy.com
https://www.niteshsynergy.com/
java-keywords-use
abstract
– Used to declare a class or method as abstract (i.e., incomplete, to be implemented by subclass).assert
– Used to test assumptions in code; throws error if condition is false (mainly for debugging).boolean
– Primitive data type that holds true
or false
.break
– Exits from a loop or switch
block.byte
– Primitive data type (8-bit signed integer).case
– Defines individual conditions inside a switch
statement.catch
– Used to catch exceptions from a try
block.char
– Primitive data type (16-bit Unicode character).class
– Used to declare a class.const
– Reserved keyword, not used in Java.continue
– Skips the current iteration in loops and jumps to the next.default
– Defines the default block in a switch
or default method implementation in interfaces.do
– Starts a do-while
loop.double
– Primitive data type for 64-bit decimal values.else
– Executes block when if
condition is false.enum
– Introduced in Java 5 to define a fixed set of constants.extends
– Indicates inheritance (class or interface extension).final
– Used to declare constants, prevent method overriding or inheritance.finally
– Block that always executes after try-catch
.float
– Primitive data type for 32-bit decimal values.for
– Loop construct for iteration.goto
– Reserved keyword, not implemented.if
– Executes block based on condition.implements
– Declares that a class implements an interface.import
– Imports classes or packages.instanceof
– Checks if an object is an instance of a class.int
– Primitive data type (32-bit signed integer).interface
– Defines an abstract type with method signatures.long
– Primitive data type (64-bit signed integer).native
– Indicates method is implemented in native code (e.g., C/C++).new
– Creates new objects.package
– Defines the package of a class.private
– Access modifier: only within the class.protected
– Access modifier: class, package, and subclass.public
– Access modifier: accessible from everywhere.return
– Returns a value from a method.short
– Primitive data type (16-bit signed integer).static
– Defines class-level fields or methods.strictfp
– Ensures consistent floating-point calculations across platforms.super
– Refers to superclass constructor or methods.switch
– Allows multi-branch conditional execution.synchronized
– Ensures only one thread can execute a block/method at a time.this
– Refers to the current object.throw
– Throws an exception.throws
– Declares exceptions a method can throw.transient
– Prevents serialization of a field.try
– Defines block to test code for exceptions.void
– Declares that a method does not return anything.volatile
– Ensures changes to a variable are visible to all threads.while
– Starts a while loop.✅ Next: Java 6 & 7
No new keywords were introduced in Java 6 or Java 7. Only new features (like try-with-resources, strings in switch, etc.) — but no new reserved words.
Java 8 introduced major features like lambdas and streams, but no new keywords were added.
👉 Functional interfaces, ::
method references, ->
lambda syntax — all added, but these are not reserved keywords.
module-info.java
) — but no new keywords.var
(in Java 10), but let’s break it down below.var
– Introduced for local variable type inference.var name = "Nitesh";
name
is of type String
.var
in lambda parameters, HttpClient
, etc.record
(preview) — not yet a keyword.sealed
– Used to restrict class inheritance.public sealed class Shape permits Circle, Square {}
permits
– Used with sealed classes to specify which classes can extend them.sealed class A permits B, C {}
record
– Became a full keyword in Java 16. Used to declare data-carrying immutable classes.record User(String name, int age) {}
Java 17 was a Long Term Support (LTS) release.
Many preview features like pattern matching improved — but no new keywords added.
This release focused on incubation features, performance, and simple web server — no keyword changes.
non-sealed
– Used to opt-out of sealing in a class hierarchy.
Example:
Became a preview feature here and finalized in Java 21.
Features like record patterns and scoped values were previewed, but no new keywords added.
Java 21 finalized many keywords introduced in earlier previews:
sealed
– Finalized.permits
– Finalized.non-sealed
– Finalized.These keywords are now part of the official Java syntax.
In total, Java 21 has 57 reserved keywords:
enum
var
sealed
record
, permits
non-sealed
(finalized)
ConcurrentHashMap
ConcurrentHashMap<K, V>
└── Segment<K, V>[] segments // Default size: 16
└── HashEntry<K, V>[] table (inside each segment)
└── Bucket (linked list of HashEntry)
Segment<K,V>
: Extends ReentrantLock
, manages one portion of the map.
HashEntry<K,V>
: Represents a single key-value pair (like Node in Java 8).
segments[0] → table[3] → HashEntry(keyA) → HashEntry(keyB)
segments[1] → table[2] → HashEntry(keyC)
Each Segment
has its own ReentrantLock
.
Concurrent writes are only allowed across different segments.
Within a segment, only one writer at a time is allowed.
Max 16 concurrent writers (default concurrencyLevel = 16
).
If multiple keys hash into the same segment → lock contention increases.
Tree-based collision handling not available — long linked lists degrade performance.
ConcurrentHashMap
ConcurrentHashMap<K, V>
└── Node<K, V>[] table
└── Each table[i] = Bucket = Linked List or TreeBin
Node<K,V>
: A key-value pair with next pointer.
TreeNode<K,V>
: Subclass used when the bucket is converted to a red-black tree.
TreeBin
: Wrapper for red-black tree logic.
Example Layout:
table[3] → Node(key1) → Node(key2) → Node(key3)
(Linked List or TreeBin)
Locking is done on the head node of the bucket, using:
synchronized (Node1) {
// Mutate bucket
}
Lock is only for mutating (put, remove) operations.
Read operations are lock-free, using volatile
and CAS
.
If a bucket has > 8 nodes, it converts to a red-black tree (TreeBin
) for performance.
TreeBin uses its own locking (synchronized
and internal ReentrantLock
) for rotations.
Java 7 and Earlier: Segment-Based Structure
ConcurrentHashMap
└── Segment[0] ──┬── table[0] ──► Entry1 ─► Entry2
├── table[1] ──► null
└── table[2] ──► Entry3 ─► Entry4
└── Segment[1] ──┬── table[0] ──► Entry5
└── ...
:
└── Segment[15]
Each Segment
contains its own internal table[]
(buckets).
Segment
uses ReentrantLock
for segment-level locking.
Default concurrency = 16 (only 16 writers can modify in parallel).
Inside each segment's table, buckets are just linked lists (Entry → Entry
).
Java 8 and Beyond: Bucket-Based Structure
ConcurrentHashMap
└── Node[0] ──► null
└── Node[1] ──► NodeA ─► NodeB ─► NodeC
└── Node[2] ──► null
└── Node[3] ──► TreeBin(root)
└── Node[4] ──► NodeX
:
└── Node[N]
No Segment[]
array — just a flat Node[] table
.
Each Node[i]
is a bucket and may contain:
A linked list of entries: NodeA → NodeB → NodeC
Or a TreeBin
(red-black tree) if entries > 8
Locking is done by:
synchronized (NodeA) // NodeA is head of bucket
Each bucket can be locked independently, allowing much higher concurrency (hundreds of parallel threads).
Feature | Java 7 (Segment-based) | Java 8+ (Bucket-based) |
---|---|---|
Top-level structure | Segment[] | Node[] (bucket array) |
Locking granularity | One lock per Segment (ReentrantLock ) | One lock per bucket (synchronized(Node) ) |
Concurrency level | Limited by number of segments (default = 16) | Limited by number of buckets (e.g., 64, 128…) |
Tree support | ❌ Not available | ✅ Yes (TreeBin for high-collision buckets) |
Read operations | Lock-free (volatile) | Lock-free (volatile + CAS) |
Aspect | Segment-based (Java 7) | Bucket-based (Java 8+) |
---|---|---|
Data Structure | Segment[] array, each with internal table | Node[] table directly |
Lock Type | ReentrantLock on Segment | synchronized(Node) on head node |
Lock Granularity | Coarse-grained (Segment-level) | Fine-grained (per bucket/bin) |
Max Concurrent Writers | Limited to #segments (default 16) | Up to number of non-colliding buckets (64, 128...) |
Memory Usage | Higher (Segment overhead) | Lower (no Segment objects) |
Read Operation | Lock-free | Lock-free |
Write Operation | Locks entire Segment | Locks only specific bucket |
Collision Handling | Linked list only | Linked list or Red-Black tree |
Tree Support | ❌ | ✅ Red-Black Tree (TreeBin) |
Resizing | Complex, Segment by Segment | Parallel-friendly resizing |
Scalability | Moderate | High |
put()
public V put(K key, V value) {
int hash = spread(key.hashCode());
int index = (n - 1) & hash;
Node<K,V> f = table[index];
if (f == null) {
// CAS insert
} else {
synchronized (f) {
// traverse linked list or tree
// insert or update
}
}
}
Lock happens only on the head node of the bucket.
Code Trace — Java 7 put()
Segment<K,V> s = segmentFor(hash);
s.lock();
try {
// access internal HashEntry[] table
// traverse and modify
} finally {
s.unlock();
}
Only one writer per segment, even if accessing different buckets inside it.
Scenario | Java 7 | Java 8 |
---|---|---|
100 threads, 100 different keys | Max 16 threads in parallel | All 100 can write if no collisions |
High collision rate | Slow due to long linked lists | TreeBin makes it faster |
Memory usage | Higher due to Segment overhead | Lower |
Thread scalability | Limited | Excellent |
Term | Segment | Bucket |
---|
Think of as | Mini-locking hash maps | Individual slots in the big table |
Used in | Java 7 and below | Java 8 and above |
Lock scope | Group of buckets | Just one bucket |
Flexibility | Less (fixed segments) | More (per-bin lock, tree support) |
Segment[i]
= A logical partition of map with its own lock
table[j]
= Bucket inside segment (Java 7) or main table (Java 8)
Entry
= Key-Value pair node (Java 7)
Node
= Key-Value pair node (Java 8)
TreeBin
= Red-Black tree used when hash collisions are too many
Your email address will not be published. Required fields are marked *