float to boolean Java


The data type boolean is incompatible for converting into any other data type. That is, a boolean value cannot be converted (or assigned to) into any other data type like char, int, float, short etc. The maximum permitted is one boolean can be assigned to another boolean, that is all. so, float to boolean is not possible either explicitly or implicitly.

Let us write an example and see what compiler says of converting float to boolean.

public class Conversions
{
  public static void main(String args[])
  {
    float f1 = 10.5f;
    boolean b = f1;        // error, float to boolean

    System.out.println(b);
  }
}


ss
Output screenshot of float to boolean Java

Following are not permitted.

float f1 = 10.5f;
boolean b = f1; // error, float to boolean

boolean b = true;
float f1 = b; // error, boolean to float

char ch = ‘A’;
boolean b =ch; // error, char to boolean

boolean b = true;
int x = b; // error, boolean to int

Following is permitted (boolean assignment).

boolean b = true;
boolean b1 = b; // permitted
System.out.println(b1); // prints true

The keyword boolean is used in comparison operations.

       float x = 10.5f;
       float y = 20.5f;
       boolean b = (x == y);
       System.out.println(b);   // prints false

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.