What is the Ideal Way to Convert an Integer to a String in Groovy

17,654

Solution 1

Does toString() work for you? It looks pretty descriptive to me, and it beats out ''+5 performance-wise.

Integer x = 5;
System.out.println(x.toString());

https://www.tutorialspoint.com/groovy/groovy_tostring.htm

Solution 2

Depending on your use case, you can do this very simply with a GString:

String str = "$number"
Share:
17,654
nmg49
Author by

nmg49

Updated on June 13, 2022

Comments

  • nmg49
    nmg49 almost 2 years

    In plain old Java, there are many ways to convert an integer to a string, such that 5 becomes "5". The answers in this post have several good suggestions, String.valueOf(number) being my favorite.

    My question is does Groovy provide a different/better way to do this? Or is the Java method still the way to go.