Why the output is false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 17

Why the output is false?

public class Program { public static void main(String[] args) { var b1 = Boolean.valueOf("1"); var b2 = Boolean.valueOf("0"); System.out.print(b1 || b2); } }

14th Oct 2019, 7:47 PM
Bogdan
Bogdan - avatar
7 Answers
+ 10
bool returns true for 1 and false for the rest. it receives the ascii codes for "0" and "1" and both are different from 1, so both variables are set to false. Tks to notqueued
21st Nov 2019, 10:01 PM
Bogdan
Bogdan - avatar
+ 9
Boolean.valueOf("True") returns Boolean.TRUE. Boolean.valueOf("TRUE") returns Boolean.TRUE. Boolean.valueOf("tRuE") returns Boolean.TRUE. Boolean.valueOf("yes") returns Boolean.FALSE. Boolean.valueOf("y") returns Boolean.FALSE. Boolean.valueOf("no") returns Boolean.FALSE. Boolean.valueOf("false") returns Boolean.FALSE. Boolean.valueOf("False") returns Boolean.FALSE. Boolean.valueOf("FALSE") returns Boolean.FALSE. for true as output compulsary the string argument contain "true" in any case upper,lower,mix ,
10th Jan 2020, 12:28 PM
Saroj Patel
Saroj Patel - avatar
+ 6
If the string argument is not the string "true" then it returns false as far as I know. For the Boolean.valueOf(String);
14th Oct 2019, 7:56 PM
Odyel
Odyel - avatar
+ 2
🌀Saroj Patel🌀🇮🇳 it s more clear now. Thank you
10th Jan 2020, 12:30 PM
Bogdan
Bogdan - avatar
+ 2
Easy, anything that is not "true" in any case is false.
11th Jan 2020, 12:53 PM
Lev Tolstoy SPB
Lev Tolstoy SPB - avatar
+ 1
Java boolean values can only equal 'true' or 'false', unlike some other languages ( 1 or 0, etc.) right? Maybe it's as simple as Java defaulting boolean values to 'false'? 🤔
22nd Oct 2019, 7:39 PM
will
will - avatar
0
Alright. Think of this in terms of boolean algebra. false is 1, true is 0. b1 is false, b2 is true. false or true = 1 OR 0 = 1 = false That should give you a good approach to tackle boolean algebra in programming
22nd Oct 2019, 6:01 PM
Shreyansh Singh
Shreyansh Singh - avatar