大佬教程收集整理的这篇文章主要介绍了Groovy与java的不同之处,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在Groovy中,下面这些包会默认引入。
- java.io.*
- java.lang.*
- java.math.bigdecimal
- java.math.bigInteger
- java.net.*
- java.util.*
- groovy.lang.*
- groovy.util.*
Groovy调用方法是在运行时进行的选择,是根据参数的类型来获取相应的方法。而java则是在编译时根据声明的类型选择相应要执行的方法。例如以下情况:
int method(String arg) {
return 1;
}
int method(Object arg) {
return 2;
}
Object o = @H_696_38@"Object";
int result = method(o);
System.out.println(result);
在java中运行结果为 2
在Groovy中运行结果为 1【因为Groovy在运行时判断出o是String类型】
在Groovy中{…}是留给闭包使用的,所以声明数组时不能像java一样使用
int[] array = { 1,2,3}
而必须使用
int[] array = [1,3]
public class Person {
String name;
public static void @H_261_80@main(String[] args) {
Person p = new Person();
p.setName(@H_696_38@"123");
System.out.println(p.getName());
System.out.println(p.Name);
}
}
对于上面的代码,在java中
p.setName(@H_696_38@"123"); System.out.println(p.getName());
这两行会报错。因为Java默认name为包访问权限,且getter setter需要自己生成。
而在Groovy中,上面的代码可以正常运行。这是因为Groovy会自动为name字段添加getter setter。
相当于如下效果:
public class Person {
@PackageScope String name
}
ARM (Automatic resource Management 自动资源管理)锁在java7是不支持的。Groovy通过闭包提供了多种实现,例如
Path file = Paths.get(@H_696_38@"/path/to/file");
Charset charset = Charset.forName(@H_696_38@"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(@H_696_38@'/path/to/file').eachLine(@H_696_38@'UTF-8') { println it }
或者更接近java的写法:
new File(@H_696_38@'/path/to/file').withReader(@H_696_38@'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)
}
}
Runnable run = () -> System.out.println(@H_696_38@"Run"); list.forEach(System.out::println);
Java 8 lambdas可以或多或少地认为是匿名内部类的写法。Groovy不支持这种语法,可以使用闭包的写法代替:
Runnable run = { println @H_696_38@'run' }
list.each { println it } // or list.each(this.&println)
在Groovy中,双引号字符串会被解释为GString值。
使用Groovy编译器 编译包含$的字符串可能会发生错误,或者会与java编译器的编译结果有些许不同(在Groovy中$用于插入文字使用,”b${a}”相当于”b”+a 包含 $的字符串如果不是正常格式的${变量名}的话会报错,如果格式正常,会被当作是GString类型)。
典型的情况:Groovy会自动转换GString和String,所以应当小心java中允许是Object类型的情形,应该对参数的真实类型进行确认。【翻译的不太对,原文是这样的:As double-quoted String literals arE interpreted as GString values,Groovy may fail with compile error or produce subtly different code if a class with String literal containing a dollar character is compiled with Groovy and Java compiler.
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.哪位英语达人给纠正一下呗】
在Groovy中,单引号的字符用于String,双引号的字符为String或GString取决于字符中是否有插值的情况:
assert 'c'.getClass()==String
assert @H_696_38@"c".getClass()==String
assert @H_696_38@"c${1}".getClass() in GString
当将单引号字符分配给char类型时,Groovy会自动将String类型(单引号字符在Groovy中默认是String类型)转换为char类型。当调用参数类型是char的方法时,我们需要明确字符的类,或者确保类型已经转换。
char a=@H_696_38@'a'
assert Character.digit(a,16)==10 : @H_696_38@'But Groovy does Boxing'
assert Character.digit((char) @H_696_38@'a',16)==10
try {
assert Character.digit(@H_696_38@'a',16)==10
assert false: @H_696_38@'Need explicit cast'
} catch(MissingMethodException E) {
println e.getmessage()
}
在上面的代码中assert Character.digit(‘a’,16)==10会报错,就是因为没有明确表明’a’的类型。
Groovy支持两种转换char类型的风格
// 对于单个字符,两种转换风格相同
assert ((char) @H_696_38@"c").class==Character
assert (@H_696_38@"c" as char).class==Character
// 对于多字符,两者有所差别
//第一种风格会报错,而第二种风格则会取第一个字符转为char类型
try {
((char) @H_696_38@'cx') == @H_696_38@'c'
assert false: @H_696_38@'will fail - not castable'
} catch(GroovyCastException E) {
println e.getmessage()
}
assert (@H_696_38@'cx' as char) == @H_696_38@'c'
assert @H_696_38@'cx'.asType(char) == @H_696_38@'c'
在java中 == 用于判读 基本类型或者对象指向 是否相同。在Groovy中,Groovy 当a、b实现Comparable时 a == b 解释为a.compareTo(b)== 0,未实现Comparable则解释为a.equals(b),检查两者是否是同一对象应该使用 is,例如 a.is(b)。
在Groovy中in、Trait也是关键字
以上是大佬教程为你收集整理的Groovy与java的不同之处全部内容,希望文章能够帮你解决Groovy与java的不同之处所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。