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

String.repeat() not working correctly in JShell

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      macOS Sonoms 14.2.1
      Java 21.0.2

      A DESCRIPTION OF THE PROBLEM :
      String.repeat() not working correctly in JShell, corresponding Java Programm produces expected results

      jshell> var buffer = new StringBuffer().repeat("BLA", 3) buffer ==> BLABLABLA
      jshell> new StringBuffer().repeat("BLA", 3) $32 ==> BLABLABLA
      jshell> var buffer = new StringBuffer() buffer ==>
           jshell> buffer = buffer.repeat("MOIN", 2)
           buffer ==>
           jshell> System.out.println(buffer)
      jshell> var sb = new StringBuffer() sb ==>
           jshell> sb.repeat('*', 10)
           $48 ==>
           jshell> sb
           sb ==>
           jshell> sb.toString()
           $50 ==> ""

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      jshell> var buffer = new StringBuffer().repeat("BLA", 3)
      buffer ==> BLABLABLA

      jshell> new StringBuffer().repeat("BLA", 3)
      $32 ==> BLABLABLA

      jshell> var buffer = new StringBuffer()
      buffer ==>

      jshell> buffer = buffer.repeat("MOIN", 2)
      buffer ==>

      jshell> System.out.println(buffer)

      jshell> var sb = new StringBuffer()
      sb ==>

      jshell> sb.repeat('*', 10)
      $48 ==>

      jshell> sb
      sb ==>

      jshell> sb.toString()
      $50 ==> ""

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      BLABLABLA
      BLABLABLA
      MOINMOIN
      **********

      ---------


      ACTUAL -
      See above, some repeat()-call produce empty results

      ---------- BEGIN SOURCE ----------
      public class StringBufferRepeat {
          public static void main(String[] args) {
              var buffer = new StringBuffer().repeat("BLA", 3);
              System.out.println(buffer);

              System.out.println(new StringBuffer().repeat("BLA", 3));

              buffer = new StringBuffer();
              buffer = buffer.repeat("MOIN", 2);
              System.out.println(buffer);

              var sb = new StringBuffer();
              sb.repeat('*', 10);
              System.out.println(sb);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      NONE => jshell not working as expected

      FREQUENCY : always


            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: