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