Click here to Skip to main content
15,795,581 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone, please I want to understand why the code below is returning a "false" from the command prompt. I passed the argument from the command line. I know that the Boolean.valueOf("a string") would convert a String into a Boolean type and saving into a primitive type boolean "b" is possible by Auto-Unboxing in java. Please is it comparing a String to a boolean primitive tpye ? Thanks for any help.

boolean b = new Boolean(Boolean.valueOf(args[0]));

This is how the argument is passed from command line, the class name is Test.
java Test 1

What I have tried:

boolean b = new Boolean(Boolean.valueOf(args[0]));
Updated 8-Mar-23 20:57pm

1 solution

Look at the documentation: Java.lang.Boolean.valueOf() Method[^]
The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".
Since "1" is not teh same as "true", "TRUE", "True" or any other combination you will always get false as a result.
Try this instead:
java Test true
Share this answer
CPallini 9-Mar-23 2:21am    
UT7 9-Mar-23 2:25am    
@OriginalGriff Got it! Thanks a lot.
OriginalGriff 9-Mar-23 3:46am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900