How To Cheque If A Order Out Is Positive Or Negative Inward Coffee - Interview Question
Write a Java programme to cheque if a lay out is positive or negative is 1 of the popular Java coding interview questions, it may await slow but programmers frequently fumble on this question. One of the tricky parts of this inquiry is that Java has multiple information type to back upward numbers similar byte, short, char, int, long, float in addition to double, out of those all are signed except char, which tin non stand upward for negative numbers. Some solution operate for int in addition to long but may non operate for floating indicate lay out e.g. float in addition to double. This is likewise a tricky Java question, another tricky indicate is considering special cases similar positive infinity, negative infinity or NaN in the instance of checking floating indicate numbers. Things larn to a greater extent than complicated when, every bit follow-up questions, interviewer lay additional atmospheric condition such every bit y'all tin non role relational operator etc. Nevertheless, Java provides several way to cheque if a lay out whether integer or floating indicate is positive or negative. In this Java tutorial, nosotros volition come across such methods of checking the sign of the lay out inward Java.
Different ways to cheque if lay out is positive or negative inward Java
Here is a quick summary of dissimilar ways I tin remember of checking the sign of lay out in addition to finding whether a lay out is positive or negative inward Java. All these methods assume that y'all volition lay especial cheque logic for treatment especial cases similar positive infinity, negative infinity, in addition to Nan in addition to assuming zip on the positive side.
1) By Converting Number to String
Convert whatever lay out into String in addition to larn the kickoff character, if its equals to "-" thus it's negative lay out otherwise it's
positive one. Though this method volition non operate for float in addition to double, if a lay out is represented inward exponential form. This could last the instance if y'all are dealing alongside large floating indicate numbers. For long and int this method of checking the sign of lay out should work. Look at next instance of string equivalent of minimum values of Integer, Long, Float in addition to Double
System.out.println(Integer.MIN_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println(Float.MIN_VALUE);
System.out.println(Double.MIN_VALUE);
-2147483648
-9223372036854775808
1.4E-45
4.9E-324
System.out.println(Long.MIN_VALUE);
System.out.println(Float.MIN_VALUE);
System.out.println(Double.MIN_VALUE);
-2147483648
-9223372036854775808
1.4E-45
4.9E-324
Only Integer in addition to Long tin last checked getting the kickoff grapheme in addition to comparison alongside "-". See How to convert Integer to String and Long to String inward Java to convert these numbers into String. By the way, this approach is delicate in addition to should non last used inward whatever production code, I own got only shared it hither because I own got seen people checking the sign of lay out similar this.
2) By using Relational Operators inward Java
Use relational operator to cheque if a lay out is positive or not, if number >=0 way a lay out is positive if number<0 way lay out is negative inward Java this should operate for double in addition to float every bit good but I haven't tested it for all values.
public static String checkSignWithRelational(double number){
if( lay out < 0){
return "negative";
}else {
return "positive";
}
}
if( lay out < 0){
return "negative";
}else {
return "positive";
}
}
Testing:
System.out.println("0.0 is " + checkSignWithRelational(0.0));
System.out.println("2.0 is " + checkSignWithRelational(2.0));
System.out.println("-2.0 is " + checkSignWithRelational(-2.0));
System.out.println("Double.POSITIVE_INFINITY is " + checkSignWithRelational(Double.POSITIVE_INFINITY));
System.out.println("Double.NEGATIVE_INFINITY is " + checkSignWithRelational(Double.NEGATIVE_INFINITY));
Output
0.0 is positive
2.0 is positive
-2.0 is negative
Double.POSITIVE_INFINITY is positive
Double.NEGATIVE_INFINITY is negative
System.out.println("2.0 is " + checkSignWithRelational(2.0));
System.out.println("-2.0 is " + checkSignWithRelational(-2.0));
System.out.println("Double.POSITIVE_INFINITY is " + checkSignWithRelational(Double.POSITIVE_INFINITY));
System.out.println("Double.NEGATIVE_INFINITY is " + checkSignWithRelational(Double.NEGATIVE_INFINITY));
Output
0.0 is positive
2.0 is positive
-2.0 is negative
Double.POSITIVE_INFINITY is positive
Double.NEGATIVE_INFINITY is negative
In my opinion, this should last the right way to cheque whether a lay out is positive or negative inward Java. Let me know if y'all guys own got seen whatever trial using the relational operator to cheque the sign of a lay out inward Java.
3) By Using Bit Shift operator
This is an option way of checking if a lay out is positive or negative inward Java in addition to used if Interviewer asks y'all non to role relational operators. Negative numbers inward Java are represented using 2's complement method in addition to since long in addition to int is signed integer along alongside byte in addition to short, close meaning chip stand upward for a sign of lay out which should last 0 for a positive lay out in addition to 1 for a negative lay out inward binary format. By using chip shift operator or a proper mask y'all tin cheque if close meaning chip is 1 or zero. Here is instance for checking int in addition to long values :
public static String checkSign(int number){
if(number == 0) return "positive";
if(number >> 31 != 0){
return "negative";
}else{
return "positive";
}
}
public static String checkSign(long number){
if(number == 0) return "positive";
if(number >> 63 != 0){
return "negative";
}else{
return "positive";
}
}
if(number == 0) return "positive";
if(number >> 31 != 0){
return "negative";
}else{
return "positive";
}
}
public static String checkSign(long number){
if(number == 0) return "positive";
if(number >> 63 != 0){
return "negative";
}else{
return "positive";
}
}
Testing for checkSign(int) method
System.out.println("0 is " + checkSign(0));
System.out.println("2 is " + checkSign(2));
System.out.println("-2 is " + checkSign(-2));
System.out.println("Integer.MAX_VALUE is " + checkSign(Integer.MAX_VALUE));
System.out.println("Integer.MIN_VALUE is " + checkSign(Integer.MIN_VALUE));
Output
0 is positive
2 is positive
-2 is negative
Integer.MAX_VALUE is positive
Integer.MIN_VALUE is negative
System.out.println("2 is " + checkSign(2));
System.out.println("-2 is " + checkSign(-2));
System.out.println("Integer.MAX_VALUE is " + checkSign(Integer.MAX_VALUE));
System.out.println("Integer.MIN_VALUE is " + checkSign(Integer.MIN_VALUE));
Output
0 is positive
2 is positive
-2 is negative
Integer.MAX_VALUE is positive
Integer.MIN_VALUE is negative
This method tin last extended to cheque double in addition to float value past times representing double in addition to 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 constituent of method declaration way returns zip if an declaration is zip , 1.0 if an declaration is greater than zip in addition to -1.0 if the declaration is less than zero. This is an overloaded method to convey float in addition to double values.
That’s all on How to cheque if a lay out is positive or negative inward Java. From looking all approaches, seems using relational operator seems close slow in addition to right solution to honor out if a lay out is positive or negative. Don’t role String approach, it's delicate in addition to solely industrial plant for only about information types.
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures in addition to Algorithms: Deep Dive Using Java
How to cheque if a lay out is a prime number lay out inward Java
0 Response to "How To Cheque If A Order Out Is Positive Or Negative Inward Coffee - Interview Question"
Post a Comment