Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8957

String with length 1, if assigned to int variable, assigns the ascii value. String with more than 1 length throws GroovyCastException

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: groovy-jdk
    • Labels:
      None

      Description

      String with length 1, if assigned to int variable, assigns the ascii value. String with more than 1 length throws GroovyCastException

      /*line 1  : case-1 */ char    c1 = '1'
      /*line 2  : case-1 */ int     n1 = c1
      /*line 3  : case-1 */ println "n1 = $n1" //prints ascii value of 1 :'n1 = 49'
      
      /*line 4  : case-2 */ String  s2 = '1'
      /*line 5  : case-2 */ int     n21 = s2
      /*line 6  : case-2 */ int     n22 = s2 as int
      /*line 7  : case-2 */ println "n21 = $n21" //prints 'n21 = 49'
      /*line 8  : case-2 */ println "n22 = $n22" //prints 'n22 = 1'
      
      /*line 9  : case-3 */ String  s3 = '10'
      /*line 10 : case-3 */ int     n3 = s3 //Throwing Cast Exception
      /*line 11 : case-3 */ println n3
      

      Case-1

      Line 3 prints `49`. It is ok as `char` is assgined to integer in line 2

      Case-2
      Line 7 prints  `n21` eventhough String is assgined in line 5... Is it right?

      Line 6 : `n22` is assigned 1 as I have written `s2 as int`. This is ok

      Case-3
      Line 10 : Throwing GroovyCastException. This is ok.

      The difference between Line-5 and Line-10 is the difference in String length. Both are Strings. The former is of length 1 and the latter is of length 2.

      So, String with length 1, if assigned to int variable, assigns the ascii value. String with more than 1 length throws GroovyCastException This looks some what different...

      Is it the acceptable behavior

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              adithyank Adithyan K
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: