This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Groovy and the fly likes Groovy - Basic Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Groovy - Basic Question" Watch "Groovy - Basic Question" New topic
Author

Groovy - Basic Question

Sri Yasasvi
Greenhorn

Joined: Nov 07, 2008
Posts: 28
Hi All,

I am new to Groovy. I am running some basic examples while learning.

Could anyone of you tell me why the last print statement is printing true.

def noParam = { -> println "I'm executed"; return "no param"}
def oneParam = { out -> println "Executed with a ${out.class} parameter"; out << "one param"}
println noParam != "no param // Printing true

Thanks,
Yasasvi

Sri Yasasvi
Greenhorn

Joined: Nov 07, 2008
Posts: 28
Here is my undrestanding.

noParam is Closure. So the statement (println noParam != "no param") is returning true.

Then i changed the statement (println noParam.toString() != "no param") . But it is still returning true. Could you please explain this.

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Unlike simple code blocks, closures have to be executed. Try invoking call() on the closure and assign the result to some variable


Mohamed Sanaulla | My Blog
Sri Yasasvi
Greenhorn

Joined: Nov 07, 2008
Posts: 28
Thank you so much Mohamed!!!


Now i understand clearly.....
 
wood burning stoves
 
subject: Groovy - Basic Question
 
Similar Threads
How do I convert a list of list into array [] []
How to print all the fields in a record
Very basic question regarding example on p135/136 of the GIA book
setHeaders
Critique Please