Groovy与java的不同之处
默认import在Groovy中,下面这些包会默认引入。 Multi-methods(运行时调度)Groovy调用方法是在运行时进行的选择,是根据参数的类型来获取相应的方法。而java则是在编译时根据声明的类型选择相应要执行的方法。例如以下情况: int method(String arg) {
return 1;
}
int method(Object arg) {
return 2;
}
对于以上两个方法进行调用 Object o = "Object";
int result = method(o);
System.out.println(result);
在java中运行结果为 2 数组初始化在Groovy中{…}是留给闭包使用的,所以声明数组时不能像java一样使用 int[] array = { 1,2,3}
而必须使用 int[] array = [1,3]
包可见性public class Person {
String name;
public static void main(String[] args) {
Person p = new Person();
p.setName("123");
System.out.println(p.getName());
System.out.println(p.name);
}
}
对于上面的代码,在java中 p.setName("123");
System.out.println(p.getName());
这两行会报错。因为java默认name为包访问权限,且getter setter需要自己生成。 public class Person {
@PackageScope String name
}
ARM锁ARM (Automatic Resource Management 自动资源管理)锁在java7是不支持的。Groovy通过闭包提供了多种实现,例如 Path file = Paths.get("/path/to/file");
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(file,charset)) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
在Groovy中可以这样写: new File('/path/to/file').eachLine('UTF-8') { println it }
或者更接近java的写法: new File('/path/to/file').withReader('UTF-8') { reader -> reader.eachLine { println it }
}
内部类Groovy的内部类以及嵌套类遵循java的规范,但是有一定的差异。Groovy使其更符合闭包的写法,坏处是访问私有字段和方法可以成为一个问题,好处是本地变量无须是final的。 静态内部类class A {
static class B {}
}
new A.B()
Groovy推荐使用静态内部类。 匿名内部类import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
CountDownLatch called = new CountDownLatch(1)
Timer timer = new Timer()
timer.schedule(new TimerTask() {
void run() {
called.countDown()
}
},0)
assert called.await(10,TimeUnit.SECONDS)
创建非静态内部类的实例在java中: public class Y {
public class X {}
public X foo() {
return new X();
}
public static X createX(Y y) {
return y.new X();
}
}
Groovy不支持y.new X()的写法,必须写成new X(y),例如下面的例子。需要注意y为null的情况,Groovy官方没有找到合适的解决方法 public class Y {
public class X {}
public X foo() {
return new X()
}
public static X createX(Y y) {
return new X(y)
}
}
lambdasjava 8支持lambdas和方法引用 Runnable run = () -> System.out.println("Run");
list.forEach(System.out::println);
Java 8 lambdas可以或多或少地认为是匿名内部类的写法。Groovy不支持这种语法,可以使用闭包的写法代替: Runnable run = { println 'run' }
list.each { println it } // or list.each(this.&println)
GStrings在Groovy中,双引号字符串会被解释为GString值。 While typically,Groovy will auto-cast between GString and String if an API declares the type of a parameter,beware of Java APIs that accept an Object parameter and then check the actual type.哪位英语达人给纠正一下呗】 String and Character字符在Groovy中,单引号的字符用于String,双引号的字符为String或GString取决于字符中是否有插值的情况: assert 'c'.getClass()==String
assert "c".getClass()==String
assert "c${1}".getClass() in GString
当将单引号字符分配给char类型时,Groovy会自动将String类型(单引号字符在Groovy中默认是String类型)转换为char类型。当调用参数类型是char的方法时,我们需要明确字符的类,或者确保类型已经转换。 char a='a'
assert Character.digit(a,16)==10 : 'But Groovy does boxing'
assert Character.digit((char) 'a',16)==10
try {
assert Character.digit('a',16)==10
assert false: 'Need explicit cast'
} catch(MissingMethodException e) {
println e.getMessage()
}
在上面的代码中assert Character.digit(‘a’,16)==10会报错,就是因为没有明确表明’a’的类型。 // 对于单个字符,两种转换风格相同
assert ((char) "c").class==Character
assert ("c" as char).class==Character
// 对于多字符,两者有所差别
//第一种风格会报错,而第二种风格则会取第一个字符转为char类型
try {
((char) 'cx') == 'c'
assert false: 'will fail - not castable'
} catch(GroovyCastException e) {
println e.getMessage()
}
assert ('cx' as char) == 'c'
assert 'cx'.asType(char) == 'c'
==操作符在java中 == 用于判读 基本类型或者对象指向 是否相同。在Groovy中,Groovy 当a、b实现Comparable时 a == b 解释为a.compareTo(b)== 0,未实现Comparable则解释为a.equals(b),检查两者是否是同一对象应该使用 is,例如 a.is(b)。 关键字在Groovy中in、trait也是关键字 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |