Java是否具有相同的比较运算符示例=== [英] Does Java have Identical Comparison Operator example ===
问题描述
Java是一个强大的静态强制转换,所以这意味着没有用于===
Java is a Strong Static Casting so does that mean there is no use for "==="
我查看过大量的文档并且没有看到相同的比较运算符。
I have looked at tons of documentation and have not seen Identical Comparison Operator.
推荐答案
===
在弱类型语言中很有用,例如Javascript,因为它验证被比较的对象是相同的类型并避免隐式转换。
===
is useful in weak typed languages, such as Javascript, because it verifies that the objects being compared are of the same type and avoids implicit conversions.
===
在Java等强类型语言中绝对没用,因为如果不编写特定的方法,就无法比较不同类型的变量。
===
has absolutely no use in a strongly typed language such as Java because you can't compare variables of different types without writing a specific method for doing this.
例如,如果要将 int
与 String
进行比较>在 Java
中,您必须编写一些特殊方法:
For example, if you want to compare an int
to a String
in Java
, you will have to write some special method as such:
boolean compareIntString(int i, String s) {
return (i == parseInt(s));
}
但这实在是太过分了。 (正如你所注意到的那样,这个方法只接受一个 int
和一个 String
。它没有' t只接受任何两个变量。在你调用它之前你知道数据类型是不同的。)
But this is pretty much overkill. (And as you'll notice, as written, this method only accepts an int
and a String
. It doesn't accept just any two variables. You know before you call it that the datatypes are different.)
重点是,虽然你可以做 i == s
在 Javascript
中,你不能做 i == s
在 Java
中,所以你不需要 ===
。
The main point is, that while you can do i == s
in Javascript
, you can't do i == s
in Java
, so you don't need ===
.
我想,简短的回答是 Java
的 ==
是 Javascript
的 ===
。如果你想模仿 Javascript
的 ==
并比较两个项目,忽略数据类型,你将不得不编写一个自定义方法,接受通用数据类型作为参数...并找出至少比较 Java
的原始数据类型的所有可能组合的逻辑......
I guess, the short answer is that Java
's ==
is Javascript
's ===
. If you want to emulate Javascript
's ==
and compare two items, ignoring data type, you'll have to write a custom method which accepts generic data types as arguments... and figure out the logic on comparing, at a minimum, all the possible combinations of Java
's primitive data types...
这篇关于Java是否具有相同的比较运算符示例===的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!