Repository navigation
Conversation
Test Results286 tests +4 283 ✅ +4 5s ⏱️ -1s Results for commit 7d1ab98. ± Comparison against base commit b42879b. This pull request skips 1 and un-skips 1 tests. |
|
@mperor ⏰ This daily topic is still waiting for your review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What you'll learn
String) before the fields are assignedList.copyOf()in the compact constructor gives the record a real defensive copy and an immutable view, closing the classic "record holds a mutable list" leakwithX) returns a new record instance with one component changed, leaving the original untouched — records have no setters, so this is the idiomatic way to get an "updated" valueequals()/hashCode()that compares the array by reference, not content, so two records holding "the same" array data can still be unequalWhy it matters
Records look like simple data holders, but the compact constructor is a real extension point for invariants, and the generated
equals/hashCode/toStringhave sharp edges (arrays) that are easy to miss until aSetorMapbehaves unexpectedly.Read more
JEP 395: Records, Record Classes (Java Language Specification, §8.10)
Quiz
Given
record Badge(String id, byte[] data) {}, ifbadge1andbadge2are built with the sameidand two differentbyte[]arrays holding identical bytes, what doesbadge1.equals(badge2)return?Answer
`false`. The compiler-generated `equals()` compares each component with `Objects.equals`/array identity semantics; for an array component that means reference equality (`Arrays.equals` is NOT used), so two distinct array instances are never equal even with identical contents — you'd need `Arrays.equals(badge1.data(), badge2.data())` to compare the bytes themselves.Closes #93