This tutorial is written for the latest version of Groovy
The same points of groovy and Java are:
0+, 3+, 4+, 6+, 8+, 10+, 12+, 13, 14, 15, 18+, 19+, 20+, 21, 22, 23, 28+, 29+, 30+, 31+, 32+
+ indicates that groovy not only covers the Java syntax, but also enhances the
The different points of groovy and Java are:
1, 2, 5, 7, 9, 11, 16, 17, 24, 25, 26, 27,33
The serial numbers listed below are in no order:
0. In groovy, you can define an untyped variable with DEF (which defines the variable aspect def as Var in JavaScript), and a method with no type to return the value, and no def in Java
Groovy:
Class Man {
def name = "Mountain Boy"
def introduce () {
Return "I ' m $name"//return can be omitted
}
}
1. The Equals method in Java corresponds to the = = in Groovy, and the = = = in Java (to determine whether to refer to the same object) corresponds to the IS method in groovy
eg.
Test1.java:
public class Test {
public static void Main (string[] args) {
String name1 = "Mountain Boy";
String name2 = new String ("Mountain Boy");
Groovy is written as name1 = = Name2
if (Name1.equals (name2)) {
System.out.println ("equal");
} else {
SYSTEM.OUT.PRINTLN ("Not Equal");
}
Groovy is written as name1.is (name2)
if (name1 = = name2) {
System.out.println ("identical");
} else {
System.out.println ("not identical");
}
}
corresponding to the Test1.java
Test1.groovy:
String name1 = "Mountain Boy"//You can also write: def name1 = "Mountain Boy" where the Def can be understood as a variable in JavaScript defined var
String name2 = new String ("Mountain Boy")//Please note that the semicolon at the end of groovy's sentence is optional and can be done without a single line of code
Written in Java as Name1.equals (name2)
if (name1 = = name2) {
System.out.println ("equal");
} else {
SYSTEM.OUT.PRINTLN ("Not Equal");
}
Java written as name1 = = Name2
if (name1.is (name2)) {
System.out.println ("identical");
} else {
System.out.println ("not identical");
}
2. Array definition in Java int[] A = {1, 2, 3}; Written in groovy int[] A = [1, 2, 3]
3. For loop in Java for (int i = 0; i < len i++) {...} can also be written as for (I in 0..len-1) {...} in groovy or for (I in 0..<len) {...}
Java:
for (int i = 0; i < len; i + +) {
Do something
}
Groovy:
for (int i =0; i < Len; i++) {
Do something
}
Or
For (i in 0. len-1) {
Do something
}
Or
For (i in 0.. < len) {
Do something
}
4. The method in Java returns write as return; or return obj; In Groovy's method, return is optional.
Java:
Public String SayHello () {
Return "Hello, Mountain Boy";
Groovy:
Public String SayHello () {
Return "Hello, Mountain Boy."
}
Or
Public String SayHello () {
"Hello, Mountain Boy."
}
Or
String SayHello () {
"Hello, Mountain Boy."
}
Or
Public SayHello () {
"Hello, Mountain Boy."
}
Or
def SayHello () {
"Hello, Mountain Boy."
}
5. The inner class in Java is the inner class, implemented in groovy with closure (closure is a feature that JAVA7 is considering, more semantically than inner class)
6. The annotations in groovy are more than Java in the first line of comment #!, other Java like the same line of comments://Multiline Comments: *//or support Javadoc/** * *
Java:
/*
* Multi-line Comment
*/
/**
* Javadoc Annotation
*/
Single-line Comment
Groovy:
#! First line comment, which enables the Unix shell to locate groovy launcher to run groovy code, for example
#!/usr/bin/groovy
/*
* Multi-line Comment
*/
/**
* Javadoc Annotation
*/
Single-line Comment
7. For-each:for (Type t:iteratable) {...} in Java5 In groovy, for (T in iteratable) {...}
Java:
for (Type t:iterable) {
Do something
}
Groovy:
for (T in iterable) {
Do something
}
8. In groovy, the switch statement is the same as in Java, but it supports more types, such as String
9. Groovy's while statement is the same as Java, but discards do-while (taking into account semantic aspects, and do-while can be replaced with other forms of circular statements, low frequency)
The string constant in Java is represented as "Hello, Mountain Boy", which can be represented in groovy as follows
Double quotes
"Hello, Mountain Boy."
Single quotes can also
' Hello, Mountain Boy '
Multi-line string
"" "Hello,
Mountain Boy "" "
Or
"Hello,
Mountain Boy
'''
Alternate string
def name = "Mountain Boy"
"Hello, ${name}"
Or
"Hello, $name" 11. Define a class in groovy with the same definition as in Java, except that classes, properties, and methods are public by default in groovy, and that the default is package in Java, and You can use Def to define methods in groovy, see comments.
Java:
public class Hello {
Private String name = "Mountain Boy";
public void SayHello () {
System.out.println ("Hello," + name);
}
Groovy:
Class Hello {
Private String name = "Mountain Boy"
public void SayHello () {
Println is the same as SYSTEM.OUT.PRINTLN () in Java
Println "Hello, $name"
}
/* SayHello can also be defined like this
def SayHello () {
Println "Hello, $name"
}
*/
}
12. Object creation is written in Java as thought T = New Thought (); It can also be written in groovy, but there are more ways to write: def t = New Thought ();
13. Static method calls are the same in Java and groovy, that is, classname.staticmethodname ();
14. Implementing interfaces and inheriting parent classes groovy is exactly the same as Java, which implements interface class ClassName implements InterfaceName {...}
Inheriting the parent class: class ClassName extends Superclass {...}
15. Define interface aspects Groovy is exactly the same as Java, i.e. interface InterfaceName {...} In groovy, the default is public
16. Regular expression constants are not in Java and are represented as/pattern/in groovy
17.Hash Constants (type JAVA.UTIL.HASHMAP) are not in Java, expressed as Def frequence in groovy = ["The": 5, "Hello": 2, "World": 2]
18. Class variable is static variable, groovy is the same as Java, static String name = "Mountain Boy", in groovy also can be written as static name = "Mountain Boy"
19. In the varargs approach, groovy is a much more expressive way than Java, as follows:
Java:
Java:
public void Varargsmethod (Type args) {
Do something
}
Groovy:
The same as in Java
def varargsmethod (Type ... args) {
Do something
}
Groovy can also use [] instead of ... to reflect the nature of varargs.
def varargsmethod (type[] args) {
Do something
}
20. Refer to the current object, groovy and Java are the same, in Java, this is represented in groovy, and in groovy, this can appear in the static scope and point to the class object of the class in which this is equivalent to Thisinstaticscope.class (Java notation) or thisinstaticscope (groovy)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.