[ 
https://issues.apache.org/jira/browse/LANG-1813?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18054093#comment-18054093
 ] 

Gary D. Gregory edited comment on LANG-1813 at 1/24/26 12:56 PM:
-----------------------------------------------------------------

I think we have an actual bug because it's confusing to be different for these 
edge cases from {{{}Arrays.hashCode(Object[]){}}}:
{code:java}
        // JRE edge cases:
        assertEquals(0, Arrays.hashCode((Object[]) null));
        assertEquals(0, Arrays.hashCode((long[]) null));
        assertEquals(1, Arrays.hashCode(ArrayUtils.EMPTY_OBJECT_ARRAY));
        assertEquals(1, Arrays.hashCode(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY));
{code}
See {{{}ArrayUtilsTest.testHashCode(){}}}.

An important difference with Arrays.hashCode() is that Arrays.hashCode() 
doesn't do a deep hashCode() on the elements themselves.


was (Author: garydgregory):
I think we have an actual bug because it's confusing to be different for these 
edge cases from {{Arrays.hashCode(Object[])}}:
{code:java}
        // JRE edge cases:
        assertEquals(0, Arrays.hashCode((Object[]) null));
        assertEquals(0, Arrays.hashCode((long[]) null));
        assertEquals(1, Arrays.hashCode(ArrayUtils.EMPTY_OBJECT_ARRAY));
        assertEquals(1, Arrays.hashCode(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY));
{code}
See {{ArrayUtilsTest.testHashCode()}}.

> `ArrayUtils#hashCode` Javadoc incorrectly states “null returns zero” (actual 
> result is 629)
> -------------------------------------------------------------------------------------------
>
>                 Key: LANG-1813
>                 URL: https://issues.apache.org/jira/browse/LANG-1813
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>            Reporter: Ivan Ponomarev
>            Priority: Trivial
>
> Since commit 8c6294dd70f986088262a6b75a382cbc9e980543 (dated Jul 19, 2003), 
> the Javadoc for {{org.apache.commons.lang3.ArrayUtils#hashCode(...)}} states 
> that a {{null}} array input “returns zero”.
> However, this has never been true in practice: the method delegates to 
> {{{}new HashCodeBuilder().append(array).toHashCode(){}}}, and 
> {{HashCodeBuilder}} returns *629* for {{null}} input (with its default 
> initial value and multiplier). This creates a long-standing discrepancy 
> between documented and actual behavior.
> Given the age of the issue and the possibility that some legacy code may rely 
> on the current behavior, changing the implementation to return 0 for {{null}} 
> could be a breaking change. The safest resolution appears to be updating the 
> Javadoc to match actual behavior (i.e., remove the “null returns zero” 
> promise).
> Reproducer: the following test fails
> {code:java}
> @Test
> void testNullArrayHashCode() {
>     // According to javadoc: "null returns zero"
>     assertEquals(0, ArrayUtils.hashCode(null));
> }{code}
>  
> Fix: https://github.com/apache/commons-lang/pull/1580



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to