Does Java have Identical Comparison Operator example ===

后端 未结 2 605
情话喂你
情话喂你 2020-12-31 11:17

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

相关标签:
2条回答
  • 2020-12-31 11:52

    No java does not have === operator. Reason is pretty well explained by nhgrif. Here is the list of operators in java and their precedence:

    enter image description here

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

    0 讨论(0)
  • 2020-12-31 11:53

    === 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.

    === 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.


    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));
    }
    

    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.)

    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 ===.


    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...

    0 讨论(0)
提交回复
热议问题