java - 默认情况下,Groovy 是否自动将 boolean 值装箱为对象?

标签 java groovy boxing

考虑这个 Java 类:

public class Demo {

    public void a(boolean a){
        System.out.println("boolean was called");
    }

    public void a(Object a){
        System.out.println("Object was called");
    }

}

Groovy 类:

class Groovy {
    static void main(String[] args) {
        def demo = new Demo()
        demo.a(true)
    }
}

输出:

Object was called

在 Groovy 中为其编写测试并传入原语 true 来调用 a(Object)

这是预期的行为吗?我如何调用其他方法? (我使用的是2.4.6版本)

仅供引用,这会导致 EasyMock 出现问题 (# 175890293 )

最佳答案

我想说这是有意的,请参阅 groovy 手册:

http://groovy-lang.org/objectorientation.html

Groovy supports the same primitive types as those defined by the Java Language Specification:

[...]

boolean type (exactly true or false)

[...]

While Groovy declares and stores primitive fields and variables as primitives, because it uses Objects for everything, it autowraps references to primitives. Just like Java, the wrappers it uses are

Table 1. primitive wrappers

Primitive type      Wrapper class
boolean             Boolean

关于java - 默认情况下,Groovy 是否自动将 boolean 值装箱为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39407516/

相关文章:

gradle - 在 gradle-5.1.1 中用什么代替 TaskInternalexecute() ?

groovy - 在 Geb 中动态设置页面 url

java - String.format(%X, num) 仅适用于整数吗?

groovy - Gradle 自动打印数字

c# - C# 中的 `x is int?` 和 `x is int` 有区别吗?

java - Java 中不必要的拆箱 - 如何重构代码?

c# - 从 int 作为对象转换为 float 需要双重转换

java - 是否可以为另一种注释类型的注释字段指定默认值?

java - java中如何让对象相互交互?

JavaFX 在 CSS 中更改禁用文本字段的文本颜色