boolean to float Java


The data type boolean is incompatible for converting into any other data type. That is, a boolean value cannot be converted into any other data type like char, int, double etc. The maximum permitted is one boolean can be assigned to another boolean. That is, boolean to float is not possible.

Following are not permitted.

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

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

  boolean b = true;
  char ch = b;                       // error, boolean to char

  char ch = 'A';
  boolean b =ch;                     // error, char to boolean

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.

  int x = 10;
  int y = 20;
  boolean b = (x == y);
  System.out.println(b);             // prints false
Let us write a simple example to see the compiler error message when boolean to float is casted.
public class Conversions
{
  public static void main(String args[])
  {
    boolean b1 = true;
    float f1 = b1;     // error
    
    System.out.println(f1);
  }
}


boolean to float
Error message when boolean to float casted

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.