double to boolean Java


double to boolean: 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, double, short etc. The maximum permitted is one boolean can be assigned to another boolean.

Following are not permitted.

       double d1 = 10.5;
       boolean b = d1;        // error, double to boolean

       boolean b = true;
       double d1 = b;         // error, boolean to double

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

       boolean b = true;
       int x = b;             // error, boolean to int
Let us see what compiler says about double to boolean casting.
public class Conversions
{
  public static void main(String args[])
  {
    double d1 = 10.5;
    boolean b = d1;        // error, double to boolean
         
    System.out.println(b);
  }
}


double to boolean
Compiler error screenshot of double to boolean Java

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.

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

Pass your comments and suggestions of how far this is helpful to you and also for the improvement of this tutorial "double to boolean"

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.