A record supplies a declared data carrier with generated members, but a record component can still reference mutable state.
Modern Java interview: records, array ownership and virtual-thread limits
Java 17+. This is a complete program using JDK classes.
Is this record deeply immutable?
No. An array component stays mutable unless the constructor and accessor enforce a copy or another ownership policy. The example copies at both boundaries. One constructor copy alone still lets the generated accessor expose that owned array to callers.
What does record equality compare?
Generated record equality compares its components according to their types. Arrays are reference-based objects under their ordinary equals method, so two records containing different arrays with equal contents are not automatically content-equal. Define a value representation or a deliberate equals/hashCode policy when that distinction matters.
Do virtual threads make CPU work faster?
They can make blocking task concurrency easier to represent, but do not increase the number of processors or remove downstream capacity constraints. A million tasks waiting for ten database connections still need an admission and deadline policy. Thread-local retention also scales with the number of active tasks.
What does a sealed hierarchy solve?
It limits permitted subtypes, supporting explicit case handling. It does not validate every constructor argument or eliminate null from callers. Keep version labels next to pattern-switch and virtual-thread examples; this ownership probe itself needs the record support supplied by Java 17.
Working program
public class RecordArrayOwnership {
record ParcelCounts(int[] values){
ParcelCounts{values=values.clone();}
public int[] values(){return values.clone();}
}
public static void main(String[] args){
int[] input={2,3};ParcelCounts recorded=new ParcelCounts(input);input[0]=9;
System.out.println(recorded.values()[0]);
int[] output=recorded.values();output[0]=8;
System.out.println(recorded.values()[0]);
System.out.println(recorded.equals(new ParcelCounts(new int[]{2,3})));
}
}Output
2
2
falseCosts and boundaries
Constructing this record and accessing its array each use O(n) copying work. That cost is an ownership decision, not an inherent cost of all records. The program makes no timing claim about virtual threads or pattern matching.
Common Mistakes
- A record reference to an array is not deep immutability.
- Array components need an explicit content-equality decision.
- Virtual threads do not remove external connection limits.
