long to boolean Java


After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to boolean as an example.

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

Following are not permitted.

int i1 = 10;
boolean b = i1;          // error, int to boolean

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

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

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

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 how compiler react with boolean casting with long to boolean.
public class Conversions
{
  public static void main(String args[])
  {
    long l1 = 65; 
    boolean b1 = l1;  // compilation error
   
    System.out.println(b1);
  }
}


long to boolean
Error message screenshot of long to boolean Java

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.