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

Character comparison incorrectly evaluated in ternary operator

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Win10
      java version "15.0.1" 2020-10-20
      Java(TM) SE Runtime Environment (build 15.0.1+9-18)
      Java HotSpot(TM) 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      See above

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Character comparison should be correctly evaluated also in a ternary operator
      ACTUAL -
      In the line
      obj= (cType=='I') ? Integer.valueOf(s) : Double.valueOf(s);

      the character comparison is evaluated as "false", which is not correct.

      ---------- BEGIN SOURCE ----------
      public class Ternary {

        public static void main(String args[]) {
          char cType= 'I';
          String s= "10";
          Object obj= s;
          System.out.println(obj);
          System.out.println(cType=='I'); // Character comparison correctly evaluated.
          obj= (cType=='I') ? Integer.valueOf(s) : Double.valueOf(s); // Incorrectly
          System.out.println(obj+" "+(obj instanceof Double)); // Unexpected

      // if-else construction works fine.
          if (cType=='I')
            obj= Integer.valueOf(s);
          else
            obj= Double.valueOf(s);
          System.out.println(obj+" "+(obj instanceof Double));
          System.out.println((cType=='I') ? "cType is I" : "cType is not I");
          System.exit(0);
        }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Use if-else construction.

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: