RFR: 8335637: Add explicit well-behaved expectations to Object.{toString, hashCode} [v2]

Joe Darcy darcy at openjdk.org
Mon Jul 8 20:40:34 UTC 2024


On Mon, 8 Jul 2024 18:04:49 GMT, Alan Bateman <alanb at openjdk.org> wrote:

> Specifying that toString returns a non-null result is good. Specifying that the methods don't throw any exceptions, or an implSpec that the methods shouldn't throw is okay.
> 
> I'm less sure about the proposed wording for resource usage. For hashCode then maybe it should say that the hashCode method may be called very frequently and should be as fast and use as few resources as possible.

Some additional background that I didn't try to put into the guidance, there are certainly contexts where calling the `toString()` or `hashCode()` methods on possibly-not-trusted objects is ill-advised. When you need a string or hash code for an object in those situations, `System.identityHashCode` and `Objects.toIdentityString` can be used. However, I think in many other contexts, and even the default context, you want to be able to call `toString` and `hashCode` on the objects you're using and have them give sensible results. The intention of this change is to make that expectation of usability explicit.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/20063#issuecomment-2215233823


More information about the core-libs-dev mailing list