2
public class Typecast {

    public static void main(String[] args) {
        int a=0;
        boolean b=(boolean)a;
        System.out.println(b);
    }
}

It gives me an error "Cannot cast from int to boolean".Can someone help?

3
  • An int can never be a boolean in Java. What are you trying to actually accomplish? Commented Oct 14, 2018 at 5:14
  • This is not a stupid question (just a duplicate). For int to boolean check @Andy Turners answer. For boolean to int you can use the ternary operator: b ? 1 : 0 Commented Jun 12, 2020 at 13:30
  • 1
    Does this answer your question? Type mismatch: cannot convert from integer to boolean Commented Jun 12, 2020 at 13:30

5 Answers 5

16

You can't cast an int to a boolean; but you can compare that int to another number, and that comparison expression will be of boolean type, for example:

boolean b = (a != 0);
Sign up to request clarification or add additional context in comments.

2 Comments

this code has one problem and it is when a has value greater than 1 for example 3 or 5 it returns 'true'
@DaniyalVaghar that's not a "problem", that's merely because of the comparison chosen. If you need different behaviour, that's fine, as stated this is just an example. But this behaviour is consistent with C-like languages (among others), where implicit Boolean conversion is allowed, where any non-zero value is considered "true"; given the question, this is likely the behaviour expected by OP.
4

Not every type can be cast to another. int to boolean is one of those that is not permitted in Java. The Java Language Specification explains what is and what isn't possible.

Comments

0

A Boolean value is one with two choices- true or false, yes or no, 1 or 0.
Boolean can't be type casted into any other data type.

1 Comment

Welcome to stack overflow! You seem to be adding further clarification to existing answers using a quote, which is good! However, it would be even better if you could state where you took this quote from, and add a link to it, which would help readers to assess the value and applicability of the quote. See also this post with tips on writing good answers. Good luck!
-1

What is your train of thought here? What are you trying to do exactly?

Are you thinking that 0 would cast to false and 1 would cast to true like in binary? As others mentioned an integer is a numeric data type and can only be cast to other numericals.

It can also be converted to a string using wrapper classes. Ie: String str = Integer.toString(a)

If you are trying to convert 0s to booleans, try this line of code to replace your line3:

b = (a != 0);

If you

Comments

-3

You cannot cast an integer to boolean in java. int is primitive type which has value within the range -2,147,483,648 to 2,147,483,647 whereas boolean has either true or false. So casting a number to boolean (true or false) doesn't makes sense and is not allowed in java.

You need to check the basic data types in java which will give you more clarity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.