为什么布尔值在某些情况下可以转换为字符串,而在其他情况下则不能?
Why a boolean can be in some cases converted to a string, and in other cases not?
我使用了一个采用 String
的方法,我想传递一个原始 boolean
。我希望它接受 boolean
并将其转换为包含 "true" 或 "false".
的字符串
假设采用以下方法:
public void method(String input) { ... }
我第一次尝试这样的事情:
boolean myBoolean = true;
method(myBoolean.toString());
这让我在编译过程中出错。但是当我添加以下额外的重载方法时,这又起作用了:
public void method(Boolean input) {
method(input);
}
我很困惑地发现,在那种情况下,调用 method(myBoolean)
工作得很好。
这是为什么?
基本类型(例如boolean
)没有方法。这就是当您尝试 boolean.toString() 时发生错误的原因。包装器 类 比不过(例如 Boolean.toString()
)。
现在说方法,如果你的方法是这样的:
public void method(String s)
那你肯定又做了这样的方法:
public void method(boolean b)
能够执行方法(布尔值)。如果没有 method(boolean b),你的编译器会说 method cannot be applied to boolean
或类似的东西。
首先要了解的是原语没有方法。至少到目前为止还没有,并且可能会随着 Project Valhalla. To be able to call the toString()
on it, you will first require to box it either by autoboxing 的实施或通过显式包装它而改变。
给定一个方法public void takeString(String input) {}
:
takeString(true)
会失败,尽管
takeString(""+true)
会起作用,因为它等同于
takeString(""+Boolean.valueOf(true).toString())
,有效。
takeString(Boolean.valueOf(true).toString())
也可以,但请注意
takeString(Boolean.valueOf(true))
将再次失败,原因与 (1) 相同。
可能很好奇,在情况 (2) 中,它能够应用 autoboxing 并隐式调用 toString()
,而在情况 (1) 中却无法这样做。
但这是有充分理由的,如 (2) 的情况,编译器很清楚方法签名将具有 String
的参数类型,因此它可以执行所需的操作隐式转换。而对于 (1),考虑到我们可以有 takeString
的多个重载版本,这样做会很危险。它只是使编译器的工作更简单,并避免了稍后添加重载版本时出现的问题。在 (2) 上失败更安全。
例如,我们不希望逻辑突然改变,因为我们添加了以下方法:public void takeString(boolean input) {}
。
也许你可以考虑添加以下方法:
public void takeString(Boolean b) {
takeString(b.toString());
}
"conversion-only" 的意图在这里很明确,并且会减少添加额外的意外逻辑的机会。
在这种情况下,为本机 boolean
参数提供重载版本可能更明智,以避免以后随着 API 的发展而出现意外。
那么除了 true.toString()
之外的所有案例都将有效。似乎是要求在 Java 中实现转换的情况,就像我们在 Scala 中实现转换一样。这将避免大量过载样板文件。
我使用了一个采用 String
的方法,我想传递一个原始 boolean
。我希望它接受 boolean
并将其转换为包含 "true" 或 "false".
假设采用以下方法:
public void method(String input) { ... }
我第一次尝试这样的事情:
boolean myBoolean = true;
method(myBoolean.toString());
这让我在编译过程中出错。但是当我添加以下额外的重载方法时,这又起作用了:
public void method(Boolean input) {
method(input);
}
我很困惑地发现,在那种情况下,调用 method(myBoolean)
工作得很好。
这是为什么?
基本类型(例如boolean
)没有方法。这就是当您尝试 boolean.toString() 时发生错误的原因。包装器 类 比不过(例如 Boolean.toString()
)。
现在说方法,如果你的方法是这样的:
public void method(String s)
那你肯定又做了这样的方法:
public void method(boolean b)
能够执行方法(布尔值)。如果没有 method(boolean b),你的编译器会说 method cannot be applied to boolean
或类似的东西。
首先要了解的是原语没有方法。至少到目前为止还没有,并且可能会随着 Project Valhalla. To be able to call the toString()
on it, you will first require to box it either by autoboxing 的实施或通过显式包装它而改变。
给定一个方法public void takeString(String input) {}
:
takeString(true)
会失败,尽管takeString(""+true)
会起作用,因为它等同于takeString(""+Boolean.valueOf(true).toString())
,有效。takeString(Boolean.valueOf(true).toString())
也可以,但请注意takeString(Boolean.valueOf(true))
将再次失败,原因与 (1) 相同。
可能很好奇,在情况 (2) 中,它能够应用 autoboxing 并隐式调用 toString()
,而在情况 (1) 中却无法这样做。
但这是有充分理由的,如 (2) 的情况,编译器很清楚方法签名将具有 String
的参数类型,因此它可以执行所需的操作隐式转换。而对于 (1),考虑到我们可以有 takeString
的多个重载版本,这样做会很危险。它只是使编译器的工作更简单,并避免了稍后添加重载版本时出现的问题。在 (2) 上失败更安全。
例如,我们不希望逻辑突然改变,因为我们添加了以下方法:public void takeString(boolean input) {}
。
也许你可以考虑添加以下方法:
public void takeString(Boolean b) {
takeString(b.toString());
}
"conversion-only" 的意图在这里很明确,并且会减少添加额外的意外逻辑的机会。
在这种情况下,为本机 boolean
参数提供重载版本可能更明智,以避免以后随着 API 的发展而出现意外。
那么除了 true.toString()
之外的所有案例都将有效。似乎是要求在 Java 中实现转换的情况,就像我们在 Scala 中实现转换一样。这将避免大量过载样板文件。