How to check if a Number is Positive or Negative in Java - Interview Question
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.
Comments
Post a Comment
-Berkomentarlah yang baik dan rapi.
-Menggunakan link aktif akan dihapus.