Hello World -! Java program written in Groovy
https://www.ibm.com/developerworks/cn/education/java/j-groovy/j-groovy.html
Click the Finish button, you should see the code snippet shown below:
1
2
3
4
5
|
class HelloWorld {
static void main(args) {
}
}
|
It looks the same in front of Java HelloWorld
strikingly similar examples. Note, however, that it does not contain a public
modifier. And, if you look closely main
parameter method, you'll notice it has no type.
Compiler
Now main
the method of adding println "Hello World"
, after the completion of the code looks like this:
1
2
3
4
5
|
class HelloWorld {
static void main(args) {
println "Hello World"
}
}
|
I learned?
OK, so this is a way to highlight the focus of tricks. Groovy is actually Java. The syntax is different - will be shorter in most cases - but Groovy code is 100% compliant with the Java byte code standards. The next section will introduce further cross two languages.
Groovy turned to Java
As already we see the first evidence of Groovy and Java code can actually interchangeable. This section will further prove this point, continue to use Groovy to build the HelloWorld
class.
Hello, Java!
In order to make you believe that Groovy is Java, now in HelloWorld
front of the class declaration and the method statement plus public
modifier, as shown below:
1
2
3
4
5
|
public class HelloWorld {
public static void main(args) {
println "Hello World"
}
}
|
Not convinced?
This code up and running exactly the same as the previous code. However, if you still not convinced, you can also args
add the parameter before String[]
:
1
2
3
4
5
|
public class HelloWorld {
public static void main(String[]args) {
println "Hello World"
}
}
|
I still have not finished
Now, it can also be println
replaced System.out.println
- and do not forget to add brackets.
1
2
3
4
5
|
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World")
}
}
|
Hello World example is now code written in Java and in front of exactly the same, but the example which is easier to write it?
Please note that the original Groovy-based HelloWorld
class without any public
modifiers, without any type (no String[]
), and provides no parentheses println
shortcuts.
Hello, Groovy!
If you prefer, you can complete this process over and back to the Java-based Hello World example, to delete the file all the contents , leaving only the System.out
line, and then delete this line System.out
and brackets. Finally, only:
1
|
println "Hello World"
|
Now, which program is easier to write it?
Run the program!
Groovy code is fully compliant with the Java byte code standards, this exercise proved this point. In Eclipse, select the Run menu option Open Run Dialog ... . Select a new Java Application configuration. Groovy ensure that the project is your project. For the Main
category, click the Search button to find HelloWorld
classes. Notice that the word class shows that Eclipse Groovy plugin has .groovy file compiled into .class files.
You can see the whole of this process in Figure 12 - If you have previously run a Java class in Eclipse, then you should be very familiar with this process.
FIG 12. Groovy codes in full compliance with the standard Java bytecode
Click the Run button, what to see? In fact, "Hello World!" Has never been able to explain the problem.
Groovy is not the type of Java code
Groovy is likely to be no rules as Java code. But in fact, Groovy just a small number of rules. The focus of this section is to use a Java programming Groovy write Java applications can not consider the specific aspects: type definitions.