SCJP 1.5
SCJP 1.5
Originally posted by robert stannard:
[QB]Hi Ashish / Marc / Krishnamoorthy,
I was expecting to see "hifalse" on the output. Why does the JVM treat a String ("hi") as a boolean and not as a String?
Hi Robert
You are correct that it must have printed hifalse as a string if you would not have assigned this to a Boolean reference variable 'b'.
If you see the Boolean API, it says as follows:
public Boolean(String s)
Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, allocate a Boolean object representing the value false.
Since you are assigned "hifalse" to a boolean reference variable, it assigns the value false as the word is neither true nor false. Everything expect true and false, will be evaluated and assigned to false.
I believe I explained correct, if any corrrections please welcome.
Kind Regards
Kris
SCJP 1.5
SCJP 1.5
line 3: Evaluates to false and so Prints out false to console
line 4: The expression is evaluated to false and assigned to Boolean obj b
line 5: printed out false to the console
Thanks<br />Dinesh
Originally posted by robert stannard:
new Boolean("anything in here") = false BUT
System.out.printf("%b", "anything in here") = true ?!
Originally posted by darshan karle:
hi all,
when i run this program in eclipse3.1 for line4 and line5 it is giving error.
saying "cannot convert from boolean to Boolean". But if run it in dos prompt it compiles and runs fine. can any body please comment on this.
regards
darshan
originally posted by Ashish Hareet
Form the java docs(note: referring 1.5, do check with others) for the 'b' it states 'If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".'
You can read more about this here - http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
scroll a little down to "Conversions"
'b', 'B' general If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".
public Boolean(String s)
Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, allocate a Boolean object representing the value false.
SCJP 1.5
Something about .... going for a swim. With this tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|