# Comparing 2 ints without using comparative operators

I was looking for questions that might be asked in a technical interview and I found this one:

Write a function that determines if two integers are equal without using any comparative operators.

I'm using Java and my solution was to wrap the ints in an Integer object, convert that Integer to a string and return:

(firstInt.toString()).equals(secondInt.toString()).


I have 2 questions:

1. Is there a better Java answer to this question?
2. Does this response fulfill the requirements and if so, is it a good one?

public class Main{

public static void main(String[] args){
log(equal(1,2)); //prints out false
log(equal(67,67)); //print out true
log(equal(-5,-5)); //prints out true
log(equal(-9,-4)); //prints out false;

}
public static boolean equal(int a, int b){
Integer first = a;
Integer sec = b;

return (first.toString()).equals(sec.toString());
}

public static void log(Object o){
System.out.println(o);
}

}


I find it highly unlikely that the interview question is designed to be answered in Java. It is more than likely referring to languages that can evaluate non-boolean value types as boolean expressions. A possible solution in JavaScript could be:

function equal(a, b) {
return !(a - b); // returns true only if (a - b) === 0
}


Is there a better Java answer to this question?

You can simplify your method by calling toString on the Integer class and passing in the integer you wish to convert.

public static boolean equal(int a, int b){
return Integer.toString(a).equals(Integer.toString(b));
}


Not that this is a better solution: a == b would be favored in any scenario.

Does this response fulfill the requirements and if so, is it a good one?

String.equals is doing a series of comparisons to verify the sequence of characters in your converted integers match; arguably this does not fulfill the requirements, but simply obfuscates it through an internal method call.

While it all makes little sense, such questions are usually designed to show how you think. The answer is not as important as the way to it. It also gives you an opportunity to say what else you know, even if it doesn't directly apply.

You can actually leave the string out:

public static boolean equal(int a, int b){
return Integer.valueOf(a).equals(Integer.valueOf(b));
}


They can argue that it's exactly the same comparison as before (with some added overhead) and you can argue that with strings, the same gets done multiple times.

Finally, you can agree that it was a stupid question.

Instead of testing x == y, we can detect whether x - y == 0 by seeing whether it triggers a division by zero error:

public static boolean equal(int x, int y) {
try {
int r = 1/(x - y);
} catch (RuntimeException e) {
return true;
}

return false;
}


## protected by Simon ForsbergSep 23 '16 at 22:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).