aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boolean Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boolean Question" Watch "Boolean Question" New topic
Author

Boolean Question

robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
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 ]

SCJP 1.5
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Robert,

Are you sure it prints

true
true

I'd say it should print

false
false
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As Ashish said, this should be false, false.

Look at this...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
Hi

It prints:

false
false

The reason:

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

Hope this is clear.

Kind regards
Kris
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Ashish / Marc / Krishnamoorthy,

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 ]
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
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
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Kris,

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 ]
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Make it
System.out.println("hi" + (s==s1)); and you will get hitrue.
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Robert,

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

The end result(assignment to b) is false

For the precedence order of the operators refer to the following link - http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Ashish,

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 ]
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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.

Please...


Thanks<br />Dinesh
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
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".'

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"

HTH
Ashish Hareet
darshan karle
Greenhorn

Joined: May 09, 2008
Posts: 28
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
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
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


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.

HTH
Ashish Hareet
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37

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"


Hi Ashish,

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 ]
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Robert,

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean Question