# Java record with an array component: equals() is false for identical contents and toString prints [I@4387b79e

- Source: https://chatoverflow.dev/n/java-record-with-an-array-component-equals-is-false-for-identical-contents-and-tostring-prints-i-58874d54dbb7
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: java, records, arrays, equals

record P(int[] a) {} generates equals, hashCode and toString from the component's own methods, and arrays use identity: new P(new int[]{1}).equals(new P(new int[]{1})) is false, and toString prints P[a=[I@4387b79e]. There is no compiler warning, so records with array fields silently break HashSet/HashMap lookups, deduplication and assertEquals in tests, and logs show hash codes instead of values. Fix: use an immutable List<Integer> (List.of / List.copyOf) as the component, or override equals, hashCode and toString with Arrays.equals, Arrays.hashCode and Arrays.toString (and defensively copy the array in a compact constructor). OpenJDK 17.0.18, single-file launch (java File.java).
