简体   繁体   English

如何在groovy中指定字符文字?

[英]How to specify character literal in groovy?

How do I specify a character literal in groovy since both 'a' and "a" result in string? 如何在groovy中指定字符文字,因为字符串中的'a'和“a”都会产生?

I do not want to declare a character variable just for this purpose. 我不想仅为此目的声明一个字符变量。

Using the as keyword is the way to make a character literal in Groovy. 使用as关键字是在Groovy中创建字符文字的方法。

'a' as char

See the discussion here at Groovy's buglist. 见讨论这里的Groovy的buglist。

If this is for a variable, you can also define the type, so: 如果这是一个变量,你也可以定义类型,所以:

import java.awt.image.*

new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB ).with {
    createGraphics().with {

        // Declare the type
        char aChar = 'a'

        // Both ways are equivalent and work
        assert fontMetrics.charWidth( aChar ) == fontMetrics.charWidth( 'a' as char )

        dispose()
    }
}

(apologies for the long example, but I had brain freeze, and couldn't think of a different standard java function that takes a char ) ;-) (对于长期的例子道歉,但我有脑冻结,并且想不到采用char的不同标准java函数);-)

This also goes against the second line of the question, but I thought I'd add it for completeness 这也与问题的第二行背道而驰,但我认为我会将其添加为完整性

There a three ways to use char literals in Groovy: 有三种方法可以在Groovy中使用char文字:

char c = 'c' /* 1 */
'c' as char  /* 2 */
(char) 'c'   /* 3 */

println Character.getNumericValue(c)           /* 1 */ 
println Character.getNumericValue('c' as char) /* 2 */ 
println Character.getNumericValue((char) 'c')  /* 3 */ 

If you assign a String literal like 'c' to a variable, Groovy does the cast implicitly (see /* 1 * /). 如果将一个字符串文字(如'c')赋给变量,Groovy会隐式执行强制转换(请参阅/ * 1 * /)。 If you want use the String literals without variables, you have to cast them by using ...as char... (see /* 2 * /) or ...(char)... (see /* 3 * /). 如果你想使用没有变量的字符串文字,你必须使用...作为char ...(参见/ * 2 * /)或...(char)...(参见/ * 3 * / )。

The usage of char literals in methods without casting them is not possible as Groovy has only String literals which must be casted to char. 在没有强制转换方法的情况下在方法中使用char文字是不可能的,因为Groovy只有String文字,必须将其转换为char。

println Character.getNumericValue('c') // compile error

This response is rather late! 这种反应相当晚! But just stumbled upon it and wanted to add some clarification. 但只是偶然发现它并希望补充一些说明。

The more accurate Answer is unlike Java, Groovy does NOT have a character literal, but you can cast a string to a character. 更准确的答案与Java不同,Groovy没有字符文字,但您可以将字符串强制转换为字符。 A literal is a value that is written exactly as it is to be interpreted, and the necessity of the type cast indicates it is NOT truly a literal. 文字是一个完全按照它来解释的值,并且类型转换的必要性表明它不是真正的文字。

Examples: 例子:

assert 'a'.class != Character.class
assert 'a'.class == String.class
assert ('a' as char).class == Character.class
assert ((char)'a').class == Character.class
char A = 'a'; // implicit coercion of string to char
assert A.class == Character.class

In contrast, both groovy and Java support numeric literals for int, long, double, and float, but do not support numeric literal for short. 相比之下,groovy和Java都支持int,long,double和float的数字文字,但简称不支持数字文字。

Examples: 例子:

assert 42.class == Integer.class
assert 42l.class == Long.class
assert 42f.class == Float.class
assert 42d.class == Double.class
assert (42 as Short).class == Short.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM