• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean Question

 
robert stannard
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert,

Are you sure it prints

true
true

I'd say it should print

false
false
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ashish said, this should be false, false.

Look at this...
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make it
System.out.println("hi" + (s==s1)); and you will get hitrue.
 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic