In this post we will see a simple Groovy "Hello world" program. You can check my previous post here. Since Groovy is an Jvm language, and runs in JVM. I need to mention on one important point about Groovy. "Some Groovy code—but not all—appears exactly like it would in Java" (keep this in mind). So we can see the "Hello World" program. The code will be :
Thats it, when you run this Groovy Script you will get the output in console as "Hello World". Note that here the println method is made as a convenient method n Groovy. What i mean convenient method is that, we need some jobs that have to be done again and again in our programming life, and convenient method makes our life easier by providing a simple syntax and solution. This code is not only short and sweet. Its more readable. Fine, lets make it more readable even, so we can do something like:
Note that in above code i have left out parenthesis and also the semicolon. Groovy can run this well as we would expect. You can see this code is very short than a normal Java Code, which can take upto 8 lines, where as groovy provides the solution in a line. This can show that Groovy code is 3 times lesser than the Java code. The reason because, if we have done this in Java then we would have defined args and many other code like public static void main(blah blah...!), which is not needed in the Hello world program(Is't it?). Groovy provides what we need in a exact manner. So i can say this following quote holds true for all Groovy programmers:
Do what you think is interesting, do something
that you think is fun and worthwhile, because
otherwise you won’t do it well anyway.
—Brian Kernighan
And also note that All Groovy code is Java but not the vice versa. So that's all for this post.
Hope this is short and sweet and comments are welcome.
println("Hello World");Thats it, when you run this Groovy Script you will get the output in console as "Hello World". Note that here the println method is made as a convenient method n Groovy. What i mean convenient method is that, we need some jobs that have to be done again and again in our programming life, and convenient method makes our life easier by providing a simple syntax and solution. This code is not only short and sweet. Its more readable. Fine, lets make it more readable even, so we can do something like:
println "Hello world"Note that in above code i have left out parenthesis and also the semicolon. Groovy can run this well as we would expect. You can see this code is very short than a normal Java Code, which can take upto 8 lines, where as groovy provides the solution in a line. This can show that Groovy code is 3 times lesser than the Java code. The reason because, if we have done this in Java then we would have defined args and many other code like public static void main(blah blah...!), which is not needed in the Hello world program(Is't it?). Groovy provides what we need in a exact manner. So i can say this following quote holds true for all Groovy programmers:
Do what you think is interesting, do something
that you think is fun and worthwhile, because
otherwise you won’t do it well anyway.
—Brian Kernighan
And also note that All Groovy code is Java but not the vice versa. So that's all for this post.
Hope this is short and sweet and comments are welcome.
Hmm, sorry, just read the last three lines, which do not make sense to me:
ReplyDeleteAll Groovy code is Java but note the vice versa
To me, there seem to be several syntactic and semantic errors in this sentences :) I would say, this is correct:
Almost all Java code is Groovy but not vice versa
...agreed?
@ice09: Yup thanks for letting me know(i have agreed and corrected it) and hope you understands this post (and others) even though i have made some typo's :D
ReplyDeleteAt byte code level they are the same. You can have groovy classes referencing java classes referencing groovy classes back and forth.
ReplyDeleteReferencing also includes Inheritance. You can have a Java class extending a Groovy class which extends a Java class which extends a Groovy class.
At source code level Java code could also be Groovy, if you're lucky. But Groovy code is almost never Java code.
That was what ice09 was talking about.
This is what ice09 was going to say.
ReplyDelete(is this better?)
@lars : Hmmmm, well i guess i have missunderstood what he have said :D
ReplyDelete