Skip to content
Variables and Strings
step 1/5

Reading — step 1 of 5

Learn

~1 min readBasics

Variables can be typed or def-typed:

def name = "Ada"        // dynamic
String other = "Bob"    // explicitly String
int age = 36            // primitive int

String interpolation with $ and ${...} — but only in double-quoted strings:

def name = "Ada"
println "Hello, $name!"           // works
println "Hello, ${name.toUpperCase()}!"
println 'Hello, $name!'           // single quotes — literal, no interpolation

Triple-quoted strings are multi-line:

def poem = """
Roses are red,
Violets are blue
"""

Reading stdin: System.console() is awkward in scripts. Use:

def line = System.in.newReader().readLine()

Or read all input as text:

def text = System.in.text

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…