“===” equivalent in Java
TL;DR
In Java there is not such a comparison operator: ===
, but ==
or equals
A longer explanation
In weakly typed languages such as JavaScript you can use the strict comparison operator (===
) because the language allows comparison between variables which have different types.
For example, in JavaScript, you won't get a compile error if you do this:
var x = 10;
var y = 'foo';
console.log(x == y); // false
And it is useful, when you want to compare variables which may hold values that are "equals" but may be of different types.
For example
var x = 10;
var y = '10';
console.log(x == y) // true
console.log(x === y) // false
In strongly typed languages such as Java, you don't need to use a strict comparison operator because the language already "handles" the type comparison.
For example:
int x = 10;
String y = "10";
System.out.println("10" == y); // true
System.out.println(x == y); // compile error : Incompatible operand types int and String
So, basically, in Java, there is no need for checking for strictness using ===
(a syntax error is reported).
In the first place, the compiler will complain when you compare values of different types using the ==
operator and conversion cannot be performed.
In the previous example of Java code, if you want to make a comparison between x
and y
you could use equals
:
int x = 10;
String y = "10";
System.out.println(y.equals(x)); // compile warning: Unlikely argument type for equals(): int seems to be unrelated to String
As a side note, notice the equals
method cannot be called on primitive types.
Some useful readings are:
- 15.21. Equality Operators
- What is the difference between a strongly typed language and a statically typed language?
- What issues should be considered when overriding equals and hashCode in Java?
I made a function which replicates the functionality of === of Javascript in Java
static boolean compareData(Object v1, Object v2)
{
if(v1 != null && v2 != null)
return (v1.getClass() == v2.getClass() && (v1.toString().equals(v2.toString())));
else
{
return (v1 == null ? v2 == null : v1.equals(v2));
}
}
I was able to pass values of any data type (except array) to this function as well as get true only if the data type and the values match else it returns false. Derived data types like List and HashMap also work.
Call to this function looks like this:
float s1 = 0.f;
float s2 = 0.1f;
System.out.println(compareData(s1, s2)); //Returns false
float s1 = 0.0f;
float s2 = 0.0f;
System.out.println(compareData(s1, s2)); //Returns true
float s1 = 0.1f;
String s2 = "0.1f";
System.out.println(compareData(s1, s2)); //Returns false
String s1 = "sdf";
String s2 = null;
System.out.println(compareData(s1, s2)); //Returns false
String s1 = null;
String s2 = null;
System.out.println(compareData(s1, s2)); //Returns true
and so on...
Update: I managed to compare arrays also, following is the code snippet, but, I haven't tested this code intensively but worked for every test case I performed.
if(s1 != null && s2 != null)
if(s1.getClass().isArray() && s2.getClass().isArray())
compareDatab = s1.getClass().equals(s2.getClass()) && (Arrays.toString(s1).equals(Arrays.toString(s2)));
else
compareDatab = compareData(s1, s2);
else
compareDatab = compareData(s1, s2);
Usage of the above snippet (Following initializations should be done prior to above code snippet,smh :P):
//s1 and s2 can be anything including Arrays and non-Array...
int[] s1 = {1,2,3};
int[] s2 = {1,2,3};
//compareDatab gives true
int[] s1 = {1,2,4};
int[] s2 = {1,2,3};
//compareDatab gives false
float[] s1 = {1,2,3};
int[] s2 = {1,2,3};
//compareDatab gives false
Where compareData() is the same function as stated prior in this answer.
Hope this proves useful to you. :)