That prints for me : — CubeJockey. Sorry it was a typo error. For it prints I have updated the snipet. But doesnt matter. Add a comment. Active Oldest Votes. Improve this answer. Louis Wasserman Louis Wasserman k 24 24 gold badges silver badges bronze badges.
When a number in java code starts with a 0, it interprets it as octal format. It's an expensive way of doing something that can simply be done with a relational operator. But it's absurdly wrong to say it doesn't work for floating point values. Of course it does. You're completely misusing Float. They are not analogues of Integer.
With such ignorance of the basics on display, I find it troubling you are writing a blog that purports to give information about Java. You also completely neglect to discuss the floating point value negative zero, which is the real sticking point between integral and floating point values when it comes to identifying negative numbers. The less-than relational operator will say that But what is the correct answer in this case?
Why are you trying to check for negative numbers at all? Your requirements are not well defined. You are kidding right? This is tricky? If I asked you this question in an interview and you answered it this way, the interview would be over immediately. No way I'd hire you. Dave Conrad, my bad. Indeed Float. Another worth noting point is that Float and Double uses sign bit to represent positive and negative number value which is different than Integer's 2's complement way. Regarding Thank you for pointing these important details.
Anonymous, I won't say it tricky for everyone but I did see many tricky point here. One of them is Float. It's not always you can do it using relational operator, sometime you got to do it using bitwise as well. Thanks for your comment. Can we use these methods to check if a double value is positive or negative? I mean checking floating point numbers e.
Post a Comment. Solution Example. Write a Java program to check if a number is positive or negative is one of the popula r Java coding interview questions , it may look easy but programmers often fumble on this question. See Primitive Data Types for more. Java stores "int" variables using 32 binary digits bits. A binary digit is either a zero or a one.
Everything to do with computers boils down to a bunch of bits at the lowest level. The leftmost bit of a Java "int" variable represents the sign, the remaining 31 bits are the number itself. If the leftmost bit is zero, the number is positive, if it's a one, the number is negative. To illustrate, this is what a positive three and a negative three look like. For the sake of simplicity, we can ignore the details of how negative numbers are represented, other than the fact that they start with a one bit.
At this point you can see that the mistake Java makes is easily detectable. If you add two "int" type numbers where the bit on the left is zero, then the result must also have a zero in the leftmost bit. At least the correct result has a zero there. If you add two positive numbers the result is also positive.
In the decimal number system the largest value that fits in three digits is , which is also 10 to the 3rd power minus one. Like Article. Last Updated : 14 Sep, Previous Trigonometric ratios of some Specific Angles. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment?
0コメント