10

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.

0

2 Answers 2

17

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

Sign up to request clarification or add additional context in comments.

Comments

13

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

String str = "$number"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.