Monday, 28 January 2019

Groovy: Methods


Methods in Groovy are defined with a return type or with the def keyword. If a method is defined with specific type, then it should return the value of that type only.

HelloWorld.groovy
int fact(n){
 if(n == 0 || n == 1) return 1
 
 return n * fact(n-1)
}

int result = fact(5)

println "Factorial of 5 is $result"

Output
Factorial of 5 is 120

In the above example, I defined a method ‘fact’ which takes an integer as input and return an integer value as output.

If you make the return type of the method as def, then you can return value of any type.


HelloWorld.groovy
def getMeSomeData(int a){

 if(a == 1){
  return 101
 }
 
 if(a == 2){
  return "You entered 2"
 }
 
 if(a==3){
  return true
 }
 
 if(a==4){
  return 3.14
 }
 
 return null
}

println "getMeSomeData(0) : ${getMeSomeData(0)}"
println "getMeSomeData(1) : ${getMeSomeData(1)}"
println "getMeSomeData(2) : ${getMeSomeData(2)}"
println "getMeSomeData(3) : ${getMeSomeData(3)}"
println "getMeSomeData(4) : ${getMeSomeData(4)}"

Output
getMeSomeData(0) : null
getMeSomeData(1) : 101
getMeSomeData(2) : You entered 2
getMeSomeData(3) : true
getMeSomeData(4) : 3.14

If you do not return a value explicitly from the method, then the value evaluated in the last line executed will be returned.


HelloWorld.groovy
def getMeSomeData(int a){

 tempResult = null
 
 if(a == 1){
  tempResult = 101
 }
 
 if(a == 2){
  tempResult = "You entered 2"
 }
 
 if(a==3){
  tempResult = true
 }
 
 if(a==4){
  tempResult = 3.14
 }
 
 finalResult = tempResult
}

println "getMeSomeData(0) : ${getMeSomeData(0)}"
println "getMeSomeData(1) : ${getMeSomeData(1)}"
println "getMeSomeData(2) : ${getMeSomeData(2)}"
println "getMeSomeData(3) : ${getMeSomeData(3)}"
println "getMeSomeData(4) : ${getMeSomeData(4)}"

Output
getMeSomeData(0) : null
getMeSomeData(1) : 101
getMeSomeData(2) : You entered 2
getMeSomeData(3) : true
getMeSomeData(4) : 3.14

You can even omit type of a parameter in method definition.


HelloWorld.groovy
def sum(a, b){
 return a + b
}

println "sum(10, 20) : ${sum(10, 20)}"
println "sum(10.1, 20.1) : ${sum(10.1, 20.1)}"
println "sum('Hello', ' World') : ${sum('Hello', ' World')}"

Output
sum(10, 20) : 30
sum(10.1, 20.1) : 30.2
sum('Hello', ' World') : Hello World

As you see the definition of sum method, I do not specified the argument types, it is decided at run time.

For example, I updated above snippet to print the argument class names.


HelloWorld.groovy
def sum(a, b){
 println "${a.class}, ${b.class}"
 return a + b
}

println "sum(10, 20) : ${sum(10, 20)}"
println "sum(10.1, 20.1) : ${sum(10.1, 20.1)}"
println "sum('Hello', ' World') : ${sum('Hello', ' World')}"

Output
class java.lang.Integer, class java.lang.Integer
sum(10, 20) : 30
class java.math.BigDecimal, class java.math.BigDecimal
sum(10.1, 20.1) : 30.2
class java.lang.String, class java.lang.String
sum('Hello', ' World') : Hello World

Points to Remember
a.   If you do not specify the access specifier (public, private, protected and default), then the method is public by default.




Previous                                                 Next                                                 Home

No comments:

Post a Comment