Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert boolean to int in Java

What is the most accepted way to convert a boolean to an int in Java?

like image 619
hpique Avatar asked Sep 25 '10 11:09

hpique


People also ask

Can boolean be converted to int in Java?

To convert boolean to integer, let us first declare a variable of boolean primitive. boolean bool = true; Now, to convert it to integer, let us now take an integer variable and return a value “1” for “true” and “0” for “false”. int val = (bool) ?

How do you convert boolean to Java?

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Can you typecast a boolean in Java?

There are two ways to convert a String to a boolean in Java, first, by using Boolean. parseBoolean() method, and second, by using Boolean. valueOf() method. The parseBoolean() method returns an equivalent boolean value of a given String, for example, if you pass "true" it will return the primitive boolean value true.


1 Answers

int myInt = myBoolean ? 1 : 0; 

^^

PS : true = 1 and false = 0

like image 189
Jonathan ANTOINE Avatar answered Oct 21 '22 13:10

Jonathan ANTOINE