Write a Java program to check if a number is positive or negative is one of the popular Java coding interview questions, it may look easy but programmers often fumble on this question. One of the tricky parts of this question is that Java has multiple data type to support numbers like byte, short, char, int, long, float and double, out of those all are signed except char, which can not represent negative numbers. Some solution work for int and long but may not work for floating point number e.g. float and double. This is also a tricky Java question, another tricky point is considering special cases like positive infinity, negative infinity or NaN in the case of checking floating point numbers. Things get more complicated when, as follow-up questions, interviewer put additional conditions such as you can not use relational operator etc. Nevertheless, Java provides several way to check if a number whether integer or floating point is positive or negative. In this Java tutorial, we will see such methods of checking the sign of the number in Java.
Different ways to check if number is positive or negative in Java
Here is a quick summary of different ways I can think of checking the sign of number and finding whether a number is positive or negative in Java. All these methods assume that you will put special check logic for handling special cases like positive infinity, negative infinity, and Nan and assuming zero on the positive side.
1) By Converting Number to String
Convert any number into String and get the first character, if its equals to "-" then it's negative number otherwise it's
positive one. Though this method will not work for float and double, if a number is represented in exponential form. This could be the case if you are dealing with large floating point numbers. For long and int this method of checking the sign of number should work. Look at following example of string equivalent of minimum values of Integer, Long, Float and Double
Only Integer and Long can be checked getting the first character and comparing with "-". See How to convert Integer to String and Long to String in Java to convert these numbers into String. By the way, this approach is fragile and should not be used in any production code, I have just shared it here because I have seen people checking the sign of number like this.
2) By using Relational Operators in Java
Use relational operator to check if a number is positive or not, if number >=0 means a number is positive if number<0 means number is negative in Java this should work for double and float as well but I haven't tested it for all values.
In my opinion, this should be the right way to check whether a number is positive or negative in Java. Let me know if you guys have seen any issue using the relational operator to check the sign of a number in Java.
3) By Using Bit Shift operator
This is an alternative way of checking if a number is positive or negative in Java and used if Interviewer asks you not to use relational operators. Negative numbers in Java are represented using 2's complement method and since long and int is signed integer along with byte and short, most significant bit represent a sign of number which should be 0 for a positive number and 1 for a negative number in binary format. By using bit shift operator or a proper mask you can check if most significant bit is 1 or zero. Here is example for checking int and long values :
This method can be extended to check double and float value by representing double and float values into long
using Double.longBitsToDouble() or Float.floatToIntBits(number).
4) Use Math.signum()
java.lang.Math provides a method called signum() which returns signum function of method argument means returns zero if an argument is zero , 1.0 if an argument is greater than zero and -1.0 if the argument is less than zero. This is an overloaded method to accept float and double values.
That’s all on How to check if a number is positive or negative in Java. From looking all approaches, seems using relational operator seems most easy and correct solution to find out if a number is positive or negative. Don’t use String approach, it's fragile and only works for some data types.
Related Java Programming Interview questions from Javarevisited Blog