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

Bug in the split function

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • None
    • 18
    • core-libs
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Mac OS

      A DESCRIPTION OF THE PROBLEM :
      The split function returns different type of Strings in case where the splitted has one or many chars.

      REGRESSION : Last worked in version 8u381

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      String str1 = "A"; // String with one char
      String str2 = "AB"; // or "ABCD..." String with at leas 2 chars
      String s1 = str1.split("")[0];
      String s2 = str2.split("")[0];
      String strA = "A";
      boolean compare1 = (strA==s1);
      boolean compare2 = (strA==s2);
      System.out.println(compare1);
      System.out.println(compare2);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      true
      true
      ACTUAL -
      true
      false

      ---------- BEGIN SOURCE ----------
      public class Main {

      public static void main(String[] args) {
      String str1 = "A"; // String with one char
      String str2 = "AB"; // or "ABCD..." String with at leas 2 chars
      String s1 = str1.split("")[0];
      String s2 = str2.split("")[0];
      String strA = "A";
              boolean compare1 = (strA==s1);
              boolean compare2 = (strA==s2);
              System.out.println(compare1);
              System.out.println(compare2);
      }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Never use == to compare two strings. Use only equals as follows:
      boolean compare1 = (strA.equals(s1));
      boolean compare2 = (strA.equals(s2));


      FREQUENCY : always


            aywang Amy Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: