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

java.lang.String.valueOf(Object) method's incorrect behaviour

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_131"
      Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
      Java HotSpot(TM) Client VM (build 25.131-b11, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      ver 7, Windows 7 Enterprise

      A DESCRIPTION OF THE PROBLEM :
      While calling the java.lang.String.valueOf(Object) method with null value the method returns "null" which is a string value. Method returns as return (obj == null) ? "null" : obj.toString(); Here "null" is a string and which makes the handling of returned value difficult as user remains unsure wheather a value was passed or not.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Call String.valueOf(null);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      null or exception
      ACTUAL -
      "null" (a String value) which is a character array.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class StringProblem {
          public StringProblem() {
      super();
          }

          public static void main(String[] args) {
      //initially when value is null
      String checkNull = null;
      String stringNull = String.valueOf(checkNull); // performing existing valueOf of function on null value

      System.out.println("Value at stringNull variable is :" +stringNull+": Performing string operation->result :"+
      stringNull.charAt(0));
      // this line should give runtime exception i.e. NullPointerException as checkNull is null,
      // valueOf(Object) returned "null" but it should return null
      // and performing any string operation will give NullPointerException.
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
          public static String valueOf(Object obj) {
              return (obj == null) ? null : obj.toString();
          }

      // when object is null, value returned should be null rather "null".

      SUPPORT :
      YES

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: