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

Add explicit non-null return value expectations to Object.toString()

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23, 24
    • core-libs
    • None
    • behavioral
    • minimal
    • Making previously implicit expectations explicit.
    • Java API
    • SE

      Summary

      Make well-behaved return value of Object.toString() explicit in the javadoc of that method.

      Problem

      In some contexts, it would be helpful to know that Object.toString() returning null was out of the interface contract for that method.

      Solution

      Add an explicit statement to Object.toString() prohibiting returning null.

      Specification

      diff --git a/src/java.base/share/classes/java/lang/Object.java b/src/java.base/share/classes/java/lang/Object.java
      index b8bfdc3e3f9..affc94479ca 100644
      --- a/src/java.base/share/classes/java/lang/Object.java
      +++ b/src/java.base/share/classes/java/lang/Object.java
      @@ -237,6 +242,10 @@ public boolean equals(Object obj) {
      
           /**
            * {@return a string representation of the object}
      +     *
      +     * Satisfying this method's contract implies a non-{@code null}
      +     * result must be returned.
      +     *
            * @apiNote
            * In general, the
            * {@code toString} method returns a string that

            darcy Joe Darcy
            darcy Joe Darcy
            Alan Bateman, Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: