Immutability in Java refers to the property of an object whose state cannot be modified after it is created. In Java, immutable objects are typically instances of classes where the attributes defined within the object cannot be changed. The most commonly used immutable class in Java is the String
class.
When you create an immutable object, any changes made to that object will result in the creation of a new object rather than modifying the original. This characteristic is beneficial as it provides simplicity and thread-safety in applications.
Here is an example of creating an immutable class in Java:
public final class ImmutablePoint {
private final int x;
private final int y;
public ImmutablePoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?