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.