Java 是否有相同的比较运算符示例 ===
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19827668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Java have Identical Comparison Operator example ===
提问by c3cris
Java is a Strong Static Casting so does that mean there is no use for "==="
Java 是强静态转换,所以这是否意味着“===”没有用
I have looked at tons of documentation and have not seen Identical Comparison Operator.
我查看了大量文档,但没有看到相同的比较运算符。
采纳答案by nhgrif
===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.
===在弱类型语言(例如 Javascript)中很有用,因为它验证被比较的对象是否属于相同类型并避免隐式转换。
===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.
===在强类型语言(例如 Java)中绝对没有用处,因为如果不编写特定的方法来执行此操作,就无法比较不同类型的变量。
For example, if you want to compare an intto a Stringin Java, you will have to write some special method as such:
例如,如果要将 anint与 a Stringin进行比较Java,则必须编写一些特殊的方法,例如:
boolean compareIntString(int i, String s) {
return (i == parseInt(s));
}
But this is pretty much overkill. (And as you'll notice, as written, this method only accepts an intand a String. It doesn't accept just any two variables. You know before you call it that the datatypes are different.)
但这几乎是矫枉过正。(你会注意到,正如所写的,这个方法只接受 anint和 a String。它不接受任何两个变量。你在调用它之前就知道数据类型是不同的。)
The main point is, that while you can do i == sin Javascript, you can't do i == sin Java, so you don't need ===.
主要的一点是,虽然你可以做i == sin Javascript,但你不能做i == sin 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's ==is Javascript's ===。如果要模拟Javascript's==并比较两个项目,而忽略数据类型,则必须编写一个自定义方法,该方法接受通用数据类型作为参数……并至少找出所有可能的比较逻辑Java的原始数据类型的组合...
回答by Juned Ahsan
No java does not have ===operator. Reason is pretty well explained by nhgrif. Here is the list of operators in java and their precedence:
没有java没有===运算符。nhgrif很好地解释了原因。这是java中的运算符列表及其优先级:


Source: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
来源:http: //docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

