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 int
s in an Integer
object, convert that Integer to a string and return:
(firstInt.toString()).equals(secondInt.toString()).
I have 2 questions:
- Is there a better Java answer to this question?
- 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);
}
}