Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Apache Groovy
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===String interpolation=== In Groovy, strings can be interpolated with variables and expressions by using GStrings:<ref>{{cite web |url=http://rajakannappan.blogspot.com.br/2009/12/groovy-strings-different-ways-of.html |title=Groovy Strings - Different ways of creating them |date=26 Dec 2009}}</ref> <syntaxhighlight lang="groovy"> BigDecimal account = 10.0 def text = "The account shows currently a balance of $account" assert text == "The account shows currently a balance of 10.0" </syntaxhighlight> GStrings containing variables and expressions must be declared using double quotes. A complex expression must be enclosed in curly brackets. This prevents parts of it from being interpreted as belonging to the surrounding string instead of to the expression: <syntaxhighlight lang="groovy"> BigDecimal minus = 4.0 text = "The account shows currently a balance of ${account - minus}" assert text == "The account shows currently a balance of 6.0" // Without the brackets to isolate the expression, this would result: text = "The account shows currently a balance of $account - minus" assert text == "The account shows currently a balance of 10.0 - minus" </syntaxhighlight> Expression evaluation can be deferred by employing arrow syntax: <syntaxhighlight lang="groovy"> BigDecimal tax = 0.15 text = "The account shows currently a balance of ${->account - account*tax}" tax = 0.10 // The tax value was changed AFTER declaration of the GString. The expression // variables are bound only when the expression must actually be evaluated: assert text == "The account shows currently a balance of 9.000" </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)