Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8280184

Add Objects.toIdentityString

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 19
    • core-libs
    • None
    • minimal
    • Add a new static method to a system class.
    • Java API
    • SE

      Summary

      Add Objects.toIdentityString.

      Problem

      At times, a toString mapping independent of the class-provided mapping needs to be used and such a method is not present in the platform.

      Solution

      Add a method with the functionality in question to java.util.Objects.

      Specification

      +    /**
      +     * {@return a string equivalent to the string returned by {@code
      +     * Object.toString} if that method and {@code hashCode} are not
      +     * overridden}
      +     *
      +     * @implSpec
      +     * The method returns a string equivalent to:<br>
      +     * {@code o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))}
      +     *
      +     * @param o an object
      +     * @throws NullPointerException if the argument is null
      +     * @see Object#toString
      +     * @see System#identityHashCode(Object)
      +     * @since 19
      +     */
      +    public static String toIdentityString(Object o) {
      +        requireNonNull(o);
      +        return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
      +    }
      +

            darcy Joe Darcy
            darcy Joe Darcy
            Alan Bateman, Mandy Chung (Inactive), Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: