Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have Identical Comparison Operator example ===

Tags:

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.

like image 278
c3cris Avatar asked Nov 07 '13 03:11

c3cris


2 Answers

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

like image 92
nhgrif Avatar answered Sep 19 '22 04:09

nhgrif


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

like image 28
Juned Ahsan Avatar answered Sep 21 '22 04:09

Juned Ahsan