Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Not A Problem
-
None
-
None
-
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