Array toString
ID |
java.array_tostring |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
code-style |
Description
Reports calls to toString() on array objects. Arrays do not override Object.toString(), producing output like [I@1a2b3c instead of the array contents.
Rationale
Calling toString() on an array yields the default Object.toString() implementation, which prints the type and hash code — not the elements. This is almost never the intended behavior and is a common source of confusing log output.
// Bad — prints "[I@1a2b3c"
int[] values = {1, 2, 3};
System.out.println(values.toString());
Remediation
Use java.util.Arrays.toString() for one-dimensional arrays or Arrays.deepToString() for multi-dimensional arrays:
// Good — prints "[1, 2, 3]"
System.out.println(Arrays.toString(values));