What is the output of the following code:
public class Main{ public static void main(String[] args) { int i = 0;//from w w w . ja va2 s .c o m i = i+ 1; boolean n = (boolean)i; System.out.println(i); System.out.println(n); } }
Compile time error: Cannot cast from int to boolean
Cast can only be used to cast compatible types
int 0 or 1 cannot represent boolean value in Java.
Check the following tutorial: [Type Conversion and Casting](Java Type Conversion and Casting).
We can use logic to convert all positive int value to true.
public class Main { public static void main(String args[]) { int i = 5;/* www. jav a 2 s .c om*/ boolean b = false; if(i > 0) { b = true; } System.out.println(b); } }