4

Since in Grails and Groovy, single-quoted Strings are different class from GStrings (double quoted Strings allowing ${variable} value injection), is it more efficient to use single quotes, except when ${variables} are being used? I am guessing that the presence of the double quotes would require parsing of the String that an ordinary single-quoted String would not require. Therefore I would expect a very slight performance hit from the extra time looking for the presence of the ${}.

So, it would seem that it would be beneficial in general to have a convention to encourage the use single quotes, unless there is a specific advantage to using the double quotes. Am I off base?

TriumphST
  • 1,194
  • 1
  • 10
  • 17

2 Answers2

8

Take a look at the first note under Double Quoted String.

Double quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.

This is possible because the interpolation is detected lazily and therefore determined whether to refer it as a GString or String. For example try to assertions below:

assert 'sample'.class.simpleName == 'String'
assert "sample".class.simpleName == 'String'
assert "sample ${1 != 2}".class.simpleName == 'GStringImpl'

Second assertion above makes it clear that double quotes are String by default until there is an interpolation (as in third assertion).

So, it does not matter if single or double quotes are used for as long as interpolation is not present. However, I personally do not think there should be any worry about performance benefits at the scale of using String or GString.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
6

is it more efficient to use single quotes, except when ${variables} are being used?

No. For the scenario where no ${variables} are used, they perform exactly the same. If there are no ${variables} in a double quoted String then the system does not create a GString. It creates a standard java.lang.String.

EDIT To Address A Separate Question Posted In A Comment Below:

It happens at compile time. The code below:

void someMethod(String a) {
    def s1 = "some string"
    def s2 = "some string with arg ${a}"
}
@groovy.transform.CompileStatic
void someOtherMethod(String a) {
    def s1 = "some string"
    def s2 = "some string with arg ${a}"
}

Compiles to this:

public void someMethod(String a)
{
    CallSite[] arrayOfCallSite = $getCallSiteArray();
    Object s1 = "some string";
    Object s2 = new GStringImpl(new Object[] { a }, new String[] { "some string with arg ", "" });
}

public void someOtherMethod(String a)
{
    String s1 = "some string";
    GString s2 = new GStringImpl(new Object[] { a }, new String[] { "some string with arg ", "" });
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • 1
    I understand that strings without the ${variable} are viewed as class String, but my question is pointed more at what happens BEFORE it is identified as a String. Isn't there a parsing process that has to identify which Class to use that would NOT be required for the single quoted String? If so, does that extra parsing occur at compile time, or run time. If runtime, then I would expect a small performance hit... – TriumphST Mar 14 '16 at 18:35
  • @TriumphST I have addressed your question in an edit to the answer above. – Jeff Scott Brown Mar 14 '16 at 19:07