Groovy is dynamically typed language, you no need to
specify the type of a variable, Groovy deduce the type from the value that a
variable hold.
HelloWorld.groovy
a = 10 b = 20 c = 30 d = 40l e = 20.05f f = 20.05 g = 'c' h = true println "a : ${a}, class : ${a.class}" println "b : ${b}, class : ${b.class}" println "c : ${c}, class : ${c.class}" println "d : ${d}, class : ${d.class}" println "e : ${e}, class : ${e.class}" println "f : ${f}, class : ${f.class}" println "g : ${g}, class : ${g.class}" println "h : ${h}, class : ${h.class}"
Output
a : 10, class : class java.lang.Integer b : 20, class : class java.lang.Integer c : 30, class : class java.lang.Integer d : 40, class : class java.lang.Long e : 20.05, class : class java.lang.Float f : 20.05, class : class java.math.BigDecimal g : c, class : class java.lang.String h : true, class : class java.lang.Boolean
Because of this dynamic nature, you can assign any type
of value to the variable.
HelloWorld.groovy
a = 10 println "a : ${a}, class : ${a.class}" a = "Krishna" println "a : ${a}, class : ${a.class}" a = true println "a : ${a}, class : ${a.class}"
Output
a : 10, class : class java.lang.Integer
a : Krishna, class : class java.lang.String
a : true, class : class java.lang.Boolean
As you see above example, I assigned 10 to the variable ‘a’,
again I assigned a string to the variable ‘a’, and again I assigned a Boolean
value to the variable ‘a’.
No comments:
Post a Comment