Dear Ranchers, can someone please explain to me how LINE1/LINE2 can work? I dont understand why (or how) it works. Im getting discombobulated. Regards Robert.
Output: true // this is a typo from the original question true // and should be amended to false/false
[ June 18, 2008: Message edited by: robert stannard ]
Boolean b = new Boolean("truE"); // line 1 String s = null, s1 = null; // line 2 System.out.println(b = "hi" + s==s1); //line 3 b = "hi" + s==s1; //## line 4 System.out.println(b);//line 5
line 1: 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.
line 2: Declares two String variables s and s1 and assigned null.
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
you're all right, I made a typo, the output should be false / false but I still dont understand why it works at all.
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? I thought the rule for concatenating Strings in a Println was that if the first item was a String then the other items would be concatenated otherwise if the first and subsequence items were non-Strings then it would try and apply addition logic to those items.
Regards Robert.
[ June 18, 2008: Message edited by: robert stannard ] [ June 18, 2008: Message edited by: robert stannard ]
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.
thanks for your explanation, it makes perfect sense. I changed the Println statement to,
System.out.println("hi" + s==s1);
and this still gives false. Any ideas why?
If I change <System.out.println("hi" + s==s1);> to System.out.println("hi" + (s==s1));
then I get the output "hitrue" and if I change the <System.out.println("hi" + s==s1);> to System.out.println(("hi" + s)==s1); then I get output "false".
could it be that the + is being evaluated before the "==", is this correct? does it make sense?
Regards Robert. [ June 18, 2008: Message edited by: robert stannard ]
b = hi + s==s1 is evaluated in the following order - (hi + s) - perform addition as String - (hi + s) == s1 - resolve equality operator - b = (hi + s) == s1 - perform assignment
thanks for your comments and the link. I read there that the "+" (additive) operator is evaluated before the "==" (equality) operator.
When I see, System.out.println("hi" + s1==s2); then just by the layout of the statement I would think its evaluated as its shown, "hi" + (s1==s2) and not ("hi" + s1) == s2.
Also on a related note why is,
new Boolean("anything in here") = false BUT System.out.printf("%b", "anything in here") = true ?!
Regards Robert.
[ June 18, 2008: Message edited by: robert stannard ] [ June 18, 2008: Message edited by: robert stannard ]
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
Krishnamoorthy Valla,
Can you please explain the Line 3 in this example how it prints false.
Originally posted by robert stannard: new Boolean("anything in here") = false BUT System.out.printf("%b", "anything in here") = true ?!
Robert,
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".'
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.
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
Your eclipse environment is using a pre 1.5 version of Java, you'll need to change your java version for your project - 1.5 or higher. While on dos prompt your java home is pointing to the 1.5 or higher version of Java.
Another reason to not use an IDE to prepare for the cert.
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".'
from the link you sent me I found this quote from the page,
'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".
but if you compare this description with that from the API docs for Boolean(String) you find
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.
I think that these two modes of operation are contradictory.
Regards Robert. [ June 21, 2008: Message edited by: robert stannard ]
In the case of System.out.printf("%b", "anything in here"), "anything in here" is not an argument of type boolean or Boolean, now apply the following statement -
'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".'
HTH Ashish Hareet
Post by:autobot
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, the permaculture playing cards