Whats a good example of casting a integer to a boolean value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Whats a good example of casting a integer to a boolean value?

I am trying to use the compareTo method; using -1, 0, and 1. I need to cast these into boolean. Not worried about the 0 though.

14th Mar 2019, 2:22 PM
Amber Janosh
Amber Janosh - avatar
1 Answer
+ 3
Assuming 1 will be cast to true and 0 to false, what should -1 be? If you consider any number which is not 0, to be true, and only 0 to be false, the simplest way would be to do (n != 0) This gives you true for 1, true for -1, and false for 0.
14th Mar 2019, 2:24 PM
Hatsy Rei
Hatsy Rei - avatar