6

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.

nmg49
  • 1,356
  • 1
  • 11
  • 28

2 Answers2

13

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

swagrov
  • 1,510
  • 3
  • 22
  • 38
9

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

String str = "$number"
doelleri
  • 19,232
  • 5
  • 61
  • 65