开发者

Does groovy "cast" a string to an integer based on its hashCode/ASCII Code?

I started coding w开发者_StackOverflowith groovy today and I notices that if I take the following code:

int aaa = "6"
log.info(aaa)

The output I get is:

54 <-- (ASCII Code for '6')

If I assign aaa with any number which is beyond the range of 0..9 I get a class cast exception.

Looks like if the string is actually a single character - groovy converts its ASCII code/hashCode.

I tried this code:

int aaa = "A"
log.info(aaa)

And the output I got was:

65 <-- (ASCII code for 'A')

What is the official reason for this? Is it because groovy automatically changes "A" into 'A'?


As Jochen says here in the JIRA; Strings of length 1 are converted to chars if needed (and by putting it into an int variable, it is assuming that that is what you want to do)

If you want to accept bigger numbers, you can do:

int a = '12345' as int

And that will convert the whole number to an int.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜