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

String concatenation operator works wrong with null

XMLWordPrintable

    • sparc
    • solaris_2.5

      s1+s2 throws NullPointerException if s1==null

      === Here is the minimized test demonstrating the bug ===
      class jdk1_1_test {
         public static void main (String args[]) {
          String s=null;
          System.out.println("some string"+s);
          System.out.println(s+"yet another string");

        }
      }
      === Test output under jdk1.1 ===
      some stringnull
      java.lang.NullPointerException
      at java.lang.StringBuffer.<init>(StringBuffer.java)
      at jdk1_1_test.main(jdk1_1_test.java:5)

      === Test output under jdk1.0.2 ===
      some stringnull
      nullyet another string

      === Here is the part of Java Language Specification 1.0 (Item 15.17.1) ===

      Now only reference values need to be considered. If the
      reference is null, it is converted to the string "null" (four
      ASCII characters n, u, l, l). Otherwise, the conversion is
      performed as if by an invocation of the toString method of the
      referenced object with no arguments; but if the result of
      invoking the toString method is null, then the string "null" is
      used instead. The toString method (§20.1.2) is defined by the
      primordial class Object (§20.1); many classes override it,
      notably Boolean, Character, Integer, Long, Float, Double, and
      String.

            wmaddoxsunw William Maddox (Inactive)
            mgorshen Mikhail Gorshenev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: