Understanding the boundaries of immutability is critical for processing streaming data. This project explores Shallow Immutability in Java Records. While a record's components are final and cannot be reassigned to new objects, if a component is a mutable object (like an array), its internal state can still be modified from the outside. This demonstration shows how a record stores a reference to an existing array, allowing external changes to reflect inside the record, highlighting the importance of defensive copying in secure system design.
- Reference Handling: Implemented a record that stores a reference to an integer array.
- State Observation: Demonstrated how modifying an external array impacts the record's data.
- Structural Integrity: Followed the Java 16+ record specification for data containers.
- Java 16+ (Records, Arrays, Reference Types)
- IntArrayRecord: A record containing a single integer array component.
- SensorLauncherApp: The entry point simulating real-time data modification.
99
Project Structure:
JavaBasics_Task_392/
├── src/
│ └── com/yurii/pavlenko/
│ ├── app/
│ │ └── SensorLauncherApp.java
│ └── sensor/
│ └── models/
│ └── IntArrayRecord.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import com.yurii.pavlenko.sensor.models.IntArrayRecord;
public class SensorLauncherApp {
public static void main(String[] args) {
int[] readings = {10, 20, 30};
IntArrayRecord container = new IntArrayRecord(readings);
readings[0] = 99;
System.out.println(container.values()[0]);
}
}package com.yurii.pavlenko.sensor.models;
public record IntArrayRecord(int[] values) {
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT