You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2019/01/16 07:30:00 UTC

[jira] [Comment Edited] (GROOVY-8957) String with length 1, if assigned to int variable, assigns the ascii value. String with more than 1 length throws GroovyCastException

    [ https://issues.apache.org/jira/browse/GROOVY-8957?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16743715#comment-16743715 ] 

Paul King edited comment on GROOVY-8957 at 1/16/19 7:29 AM:
------------------------------------------------------------

Groovy always coerces Strings of length 1 to a character when required.
{code:java}
String s = '1' // string containing '1'
char c  = '1'  // character '1'
int i = '1'    // ascii value for '1'
{code}


was (Author: paulk):
Groovy always coerces Strings of length 1 to char when required.
{code}
String s = '1' // string containing '1'
char c  = '1'  // character '1'
int i = '1'    // ascii value for '1'
{code}

> String with length 1, if assigned to int variable, assigns the ascii value. String with more than 1 length throws GroovyCastException
> -------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-8957
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8957
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>            Reporter: Adithyan K
>            Priority: Major
>
> String with length 1, if assigned to int variable, assigns the ascii value. String with more than 1 length throws GroovyCastException
> {code}
> /*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
> {code}
> 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
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)