How to specify character literal in groovy?

groovy

Solution

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`) ;-)

This also goes against the second line of the question, but I thought I'd add it for completeness

Problem

How do I specify a character literal in groovy since both 'a' and "a" result in string? I do not want to declare a character variable just for this purpose.

Original source