• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean b = false | true;

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question if that's ok.......Is there a point to this statement (with or without the short circuit)?

boolean b = false | true;

Or is it just a sort of side effect of this (more useful) code being allowed:

boolean b = methodA() | methodB();

Maybe it's a stupid question, but it just seems a bit odd to me....

Regards,

Nick
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it's the | operator and not the || operator you are asking about?
 
Greg Charles
Sheriff
Posts: 3002
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there's no point to:



It's equivalent to:



I'm not sure what you mean by side effect in this context, but maybe you mean that it's legal syntax because



... is legal syntax, and would have a different effect from the || (shortcut) version in the specific case where methodA() returns false and methodB() has some side effect. I'd agree that's why the first line is legal code, even though there's no point to it.

Actually, the single | (non-shortcut) boolean operator is one of those things that in theory could be useful, but in practice is rarely or never used. Other examples are falling through in switch statements or labeled breaks.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:boolean b = methodA() | methodB();

Maybe it's a stupid question, but it just seems a bit odd to me....

I don't see the odd part, so you have to help me with that. If your business logic requires a conditional OR for two conditions, then you'll certainly use the short-circuit version. The bitwise operators are more used for bitwise operations and are less commonly used.

I can think about something likeAnd if you want an example for the conditional AND, I've got one as well

Hope that makes some sense!

Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys, I guess I wasn't clear enough!

I mean when actually using the literals 'true' and 'false' as the operands. There doesn't seem to be any point to it

eg:
boolean b = true | false;
boolean b = false || false;
boolean b = true | true;

(or any other combination, whether using |, ||, & or &&). none of those statements are useful or make any particular sense (as far as I can see), but are valid.

Obviously unlike using methods as the operands - I completely understand that. That's what I meant by 'side effect' - that the use of literals works because the compiler is expecting to see a boolean, whether from a useful method, or a pointless literal.

Hope that makes more sense, and thanks for the replies!

Nick





 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only place you'd ever see this would be an exam/mock/test. (I hope!)
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:The only place you'd ever see this would be an exam/mock/test. (I hope!)


It was, and although I definitely know how boolean b = false | true; (for example) would resolve, it did throw me for a bit at the time. And I then started wondering why the syntax is even valid.

The more I think about it though, the more pointless the syntax (and my question itself) seems.

I guess it's just the way it is!

Regards,

Nick
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well its just an abstraction (for the purpose of testing knowledge) from the more useful variant that calls two methods (or perhaps tests two variables) which you referred to in your first post.
They've simply taken that real world possibility and stripped it down to bare essentials for the purpose of testing your knowledge of the binary conditional operators that would be found in that situation.
(edit: but the syntax itself is not pointless, just this rendering of it is)
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:The only place you'd ever see this would be an exam/mock/test. (I hope!)

You wish you would see something as easy ason the (actual) exam. You'll probably facing with much more complicated statements likeAnd if you really want to go crazy and have some moments you can throw a few loop statements in there as well. Fun guaranteed!

Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:eg:
boolean b = true | false;
boolean b = false || false;
boolean b = true | true;

(or any other combination, whether using |, ||, & or &&). none of those statements are useful or make any particular sense (as far as I can see), but are valid.

Of course these statements make no sense, but they are definitely valid. Why would it not be valid? And if you would like (or expect) them to be invalid, what about these onesThese statements are very similar, because instead of "boolean arithmetic", it's "integer arithmetic".

And don't forget that you write a code snippet like thisBut because DEBUG is a compile-time constant, the compiler turns this into

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
nick woodward wrote:eg:
boolean b = true | false;
boolean b = false || false;
boolean b = true | true;

(or any other combination, whether using |, ||, & or &&). none of those statements are useful or make any particular sense (as far as I can see), but are valid.

Of course these statements make no sense, but they are definitely valid. Why would it not be valid? And if you would like (or expect) them to be invalid, what about these onesThese statements are very similar, because instead of "boolean arithmetic", it's "integer arithmetic".

Hope it helps!
Kind regards,
Roel


i dunno, my brain just initially looks at b = true || false, and goes "yeah, well of course it's either true or false, and???" - rather than looking at how the operator acts on the literals and what is assigned (I know and understand it, but I'm just explaining my initial though process!)

oh and:

x = 4;
y = 4;
z = 105;
b = false;

?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:oh and:

x = 4;
y = 4;
z = 105;
b = false;

That's exactly what I had as well, so it has to be spot-on
 
Krishna Srinivasan
Ranch Hand
Posts: 1876
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may not use the similar statement in the real world scenario. But, if you are preparing for the exams, it is perfectly possible to see this kind of statements to test your knowledge.

Roel had provided good explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic