why does this print true?

Guillermo Ishi
Ranch Hand
Posts: 789
I would expect this to print false, since it is not the "short circuit" OR but it prints true.

Henry Wong
author
Marshal
Posts: 22124
88
Guillermo Ishi wrote:I would expect this to print false, since it is not the "short circuit" OR but it prints true.

The result of true OR false is true. For an OR operator to return false, both operands must be false.

Henry

Guillermo Ishi
Ranch Hand
Posts: 789
Ok, my train of thought would have had whatever the last term was being the thing printed (for non-short circuit). Never mind...

Liutauras Vilda
Bartender
Posts: 2812
112
Hi Guillermo,

I prepared one example for you, so you could see better the difference between (Logical OR ||) and (Bitwise OR |).
OUTPUT:
Expression: true
b: false
Expression: true
b: true

So, from the example you could see, that in both cases expression evaluates to "true", since first operand has value of "true". It means, truth tables are the same for both Logical and Bitwise OR.
The difference is, that in first case, within "if" condition check "b" suppose to be assigned to "true", but indeed it is not being assigned, since Logical OR is a short-circuit and once first operand is known as "true", second operand is not being evaluated.
Within second "if" whole expression evaluates to "true", as well as the second operand being evaluated and assigned to "true", so, you see the outputs.

Guillermo Ishi
Ranch Hand
Posts: 789
Liutauras Vilda wrote:Hi Guillermo,

I prepared one example for you, so you could see better the difference between (Logical OR ||) and (Bitwise OR |).

Thanks; I was having a brain fart and thinking that with the non- short circuit all the return values are evaluated or something, but it's actually the operations that are all evaluated.

But you might misunderstand bitwise OR. The "|" is bitwise or non-short circuit OR depending on the operands.
( 1 | 2) prints 3 is the bitwise operation - When you OR 0000 0001 with 0000 0010 you get 0000 0011 = 3
(true | false) is the is the non-short circuit OR
Which the "|" is depends on the operand (argument) types.

Liutauras Vilda
Bartender
Posts: 2812
112
Guillermo Ishi wrote:But you might misunderstand bitwise OR.
No I didn't

Guillermo Ishi wrote:The "|" is bitwise or non-short circuit OR depending on the operands.
I didn't state opposite.

Guillermo Ishi wrote:I would expect this to print false, since it is not the "short circuit" OR but it prints true.
I provided example based on your confusion in these circumstances.

Guillermo Ishi
Ranch Hand
Posts: 789
You were calling it the bitwise operator but were supplying it boolean operands only. I guess a name is a name though.

Liutauras Vilda
Bartender
Posts: 2812
112
Probably more correct would be to call it Boolean OR (|) if operating with boolean values. And Bitwise OR (|) when OR'ing bits.
But then, how would you call operator (&) if you wouldn't know the values at the beginning. In fact, this operator is called Bitwise AND, and (|) Bitwise OR.

You can check in Oracle's website (<- link) to convince yourself, how the operators are called

Roel De Nijs
Sheriff
Posts: 10666
144
Liutauras Vilda wrote:In fact, this operator is called Bitwise AND, and (|) Bitwise OR.

You can check in Oracle's website (<- link) to convince yourself, how the operators are called

I checked Oracle's Summary of Operators and it's in fact Bitwise inclusive OR

And just for completeness: for the exam you'll need to know the & and | operator applied to boolean values (and thus the difference with the short-circuit operators), but not their bit-twiddling capabilities.

Liutauras Vilda
Bartender
Posts: 2812
112
Roel De Nijs wrote:I checked Oracle's Summary of Operators and it's in fact Bitwise inclusive OR

You're right about that how they named in Oracle's Summary of Operators.

But in my book "OCA Java SE 7 Programmer I Study Guide" by Edward Finegan and Robert Liguori (just checked again, page 98), calls them just "Bitwise AND and Bitwise OR".

Guillermo Ishi
Ranch Hand
Posts: 789
Roel De Nijs wrote:
I checked Oracle's Summary of Operators and it's in fact Bitwise inclusive OR

Well...If you're going to get all hardcore you'd better get it right! So let's look at the specification, not the ahem...summary.

15.22. Bitwise and Logical Operators
The bitwise operators and logical operators include the AND operator &, exclusive OR operator ^, and inclusive OR operator |.
...
15.22.2. Boolean Logical Operators &, ^, and |
...
15.22.1. Integer Bitwise Operators &, ^, and |
...

Roel De Nijs
Sheriff
Posts: 10666
144
Guillermo Ishi wrote:
Roel De Nijs wrote:
I checked Oracle's Summary of Operators and it's in fact Bitwise inclusive OR

Well...If you're going to get all hardcore you'd better get it right! So let's look at the specification, not the ahem...summary.

No intention to get all hardcore at all! Just did what Liutauras suggested: check the link to convince myself how the operators are called. Nothing more, nothing less.

Liutauras Vilda
Bartender
Posts: 2812
112
Guillermo Ishi wrote:Well...If you're going to get all hardcore you'd better get it right! So let's look at the specification, not the ahem...summary.
Guillermo, you should read further in JLS, next sentence..

15.22.2. Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean.

Think of it as about a "Brick house", if some people lives in that house, you'd call it "Living house", if not "Non living house", but, it doesn't change the fact, that it is a "Brick house".
So, the summary of operators supported by the Java programming language in this website, indeed is correct.

Guillermo Ishi
Ranch Hand
Posts: 789
Liutauras Vilda wrote:
Guillermo Ishi wrote:Well...If you're going to get all hardcore you'd better get it right! So let's look at the specification, not the ahem...summary.
Guillermo, you should read further in JLS, next sentence..

15.22.2. Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean.

Think of it as about a "Brick house", if some people lives in that house, you'd call it "Living house", if not "Non living house", but, it doesn't change the fact, that it is a "Brick house".
So, the summary of operators supported by the Java programming language in this website, indeed is correct.

No. Sorry. "15.22.2. Boolean Logical Operators &, ^, and |" means what is says. They are introduced first as bitwise operators so they are referred to that way again simply to identify them.

They are two groups of overloaded operators that look the same but are distinct and independent. Depending on the operands, you are dealing with either the "boolean logical operators" or the "integer bitwise operators".