
org.sonar.l10n.java.rules.java.S2116.html Maven / Gradle / Ivy
The newest version!
Why is this an issue?
The purpose of the hashCode
method is to return a hash code based on the contents of the object. Similarly, the purpose of the
toString
method is to provide a textual representation of the object’s contents.
Calling hashCode()
and toString()
directly on array instances should be avoided because the default implementations
provided by the Object
class do not provide meaningful results for arrays. hashCode()
returns the array’s "identity hash
code", and toString()
returns nearly the same value. Neither method’s output reflects the array’s contents.
How to fix it
Use relevant static Arrays
method.
-
Arrays.hashCode
or Arrays.deepHashCode
-
Arrays.toString
or Arrays.deepToString
Code examples
Noncompliant code example
public static void main(String[] args) {
String argStr = args.toString(); // Noncompliant
int argHash = args.hashCode(); // Noncompliant
}
Compliant solution
public static void main(String[] args) {
String argStr = Arrays.toString(args); // Compliant
int argHash = Arrays.hashCode(args); // Compliant
}
Resources
Documentation
Articles & blog posts
© 2015 - 2025 Weber Informatics LLC | Privacy Policy