• Post Reply Bookmark Topic Watch Topic
  • New Topic

Meaning of &  RSS feed

 
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have conflicting info on this operator. The book by Esteban Herrera (Java practice questions - OCA) says that the left and right operands for & operator must be boolean (page 189, "the & operator only works with booleans........"). Netbeans IDE agrees and gives an error if i type in a int as one operand (INSIDE an if condition). But the following line compiles fine :

Also, tutorialspoint says & operator is "binary AND" and has an example (which i don't understand). I am familiar with bitwise AND operator in C language, which is denoted by &. Does JAVA give the same bitwise AND meaning to this operator ? Do all the other bitwise operators of C (like ~) have the same meaning in Java too ? Please elaborate (you can use the same example given in tutorialspoint. ) Thanks.

 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(INSIDE an if condition)

An if condition must be boolean. That error has little to do with the & operator.
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know an if condition has to evaluate to a boolean. But the exact line used by the above mentioned book is ....

So, first i need to understand the operator and expected operands. Netbeans IDE gives error for the above statement (bad operand types for &). If both operands are boolean, then Netbeans accepts and does not give an error. If this is a bitwise & operator (like in C), having booleans does not make any sense to me.

(Note to "Bear" : I edited my original post, after you replied. please re-read if you are still in this thread).


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Operator precedence!

if ((count & count%2) == 0 ) ...
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Under operator precedence, i see == has higher precedence than bitwise & operator. So, it would bind like
if ( count & ((count%2) == 0 ) ) ...
I also think the wording is wrong in the book in saying "only booleans have to be operands". I would like to understand how the bitwise & operator works given boolean operands. For an if condition, the logical AND would be &&, so how come the bitwise & operator works in place of &&. Compiler accepts a condition like if (true & false).....
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you worked out what that predicate evaluates to?
If you look in the Java Language Specification, you find that & | and ^ are overloaded to operate on booleans as well as integers, but you are right that you should normally use && on booleans.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:so how come the bitwise & operator works in place of &&. Compiler accepts a condition like if (true & false)...

Because, as was previously mentioned, '&' also works with booleans.

However, the two operators do NOT work the same way - specifically, they are not evaluated the same way - as you'll soon discover if you put boolean expressions on either side of them, eg:
if (name.equals("fred") && (++age == 100))
and
if (name.equals("fred") & (++age == 100))

I'll leave you to find out how they differ. Suffice to say that the first one may produce some odd results.

I should add that both are examples of something you should never try in real life.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . Suffice to say that the first one may produce some odd results. . . .
The second way will produce odd results, too.

Did you ever work out what ((count & count%2) == 0 ) equates to?
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested both cases mentioned above with the following code piece and the result was as expected in the print. Both expressions should result in a true or false situation ?


Campbell - I checked operator precedence, and confirmed that is not the problem. (as mention in message above).
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:I tested both cases mentioned above with the following code piece and the result was as expected in the print. Both expressions should result in a true or false situation ?



And what happens when you change the first line to ...

Also... Winston asked how they differ? Not whether they compile or not. Of course, they result in true or false. They wouldn't compile otherwise.

Henry
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for your reply. Henry - if you change name to "not fred" then you won't see the print outs; So i don't see your point, henry.
I don't understand winston's point in giving this code piece because the evaluation result is same using & as well as &&. Now it is clear to me that & is bitwise operator and is overloaded for boolean operands. Winston's point of 'how they evaluate' can be understood only if i step into java source code, which is too much for my level.
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if I'm getting Henry's and Winston's point right, but I think if you changed your code as follows



and ran it first as is and then by changing the first line as follows



You'd see a difference in the output. Now try to reason why the second output does not print the age statement.
The age is 25 and you'd imagine the count to be 2 and hence it should have printed the second line in the second case.
But it doesn't.

Third case : Now if you change your code as follows.



the second println statement gets printed. But you only changed the '&&' to an '&'.
So there must be some difference in the way && is evaluated and in the way & is evaluated.
Can you try and spot the reason for this difference in behavior?
Can you figure why in the third case the second line gets printed but it doesn't get printed in the second case.

Some of the constructs employed in the illustration aren't good programming practices. But they are used here just for the illustration.
We will come back to that part later.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote: I don't understand winston's point in giving this code piece because the evaluation result is same

It's the same for what you're testing, but the two operators do NOT work the same.

I'll put you out of your misery:
The first statement (the one with the '&&') will NOT evaluate the right-hand expression unless it has to. It's one of the reasons that it's sometimes called a "short-circuit" operator. So, if 'name' is not "fred", 'age' will never get incremented.

The '&', on the other hand, always evaluates both sides, even if it isn't logically necessary to do so, because it's actually doing a bitwise AND on the boolean results.

Simply put:
1. You should almost always use '&&' with logical expressions, or ones that involve booleans. '&' is strictly for bitwise (ie, numeric) operations.
2. You should NEVER write code like those examples.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . You should NEVER write code like those examples.

HIH

Winston
Aren't they wonderfully confusing! And they get worse as you read through the thread!

And how many people have worked out what the original predicate reduces to?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Aren't they wonderfully confusing! And they get worse as you read through the thread!

Very true. Unfortunately, I couldn't think of a better way to illustrate the difference between the way the two operators work.

If anyone has one, I'd love to bookmark it.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . . And how many people have worked out what the original predicate reduces to?
I am pretty sure Winston and I have worked it out, but what about the rest of you?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . Unfortunately, I couldn't think of a better way to illustrate the difference between the way the two operators work. . . .
Because using & | or ^ on booleans so as to get side‑effects from the right half of the expression is never good practice, there will never be a “good” way to describe those operators.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Because using & | or ^ on booleans so as to get side‑effects from the right half of the expression is never good practice, there will never be a “good” way to describe those operators.

Actually, I seem to remember seeing quite a good one that involved method calls, but darned if I can recall it exactly. Bloody Alzheimers...

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not Alzheimer's; that's just bl**d* annoying
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Heena, your example brought out the point clearly.

Campbell - I am not sure which predicate you were referring to as 'original predicate' because soon after the OP, the expression got changed for rest of thread. If you are referring to


then it was clear to me what this does, so i didn't present it. This will perform a bitwise AND on count and (count+1). If you were referring to the predicate

then, this case is also was clear to me. Due to operator precedence the above would be equivalent to

In this case, if count is odd integer, then the right hand side operand evaluates to 'true'.
If count is an even integer, then the right hand side operand would evaluate to 'false'. In both cases, we have a integer as left side operand and a boolean as right side operand.
So, the compiler gives an error.

In all cases a bitwise AND is performed. I wasn't aware of the fact that & evaluates both operands (no matter what); this became clear after Heena's example.
Thanks all, for bringing out this point. This would make a perfect question on the OCA exam.

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote: . . .
That isn't a predicate.
. . . . . .
That won't compile, so it isn't a predicate, but if you make it compile with…it becomes a predicate. You have nearly worked it out, I think

I don't remember there being a ++ in the original post. By the way, are you sure that is exactly how the code appears in the book? Have you checked the book's errata page/website?
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell - i simplified the expression a bit for the purpose of focusing on my question. I searched for errata on the book, but there is none to be found on internet. The expression occurs in a question, and the right answer is to pick the answer "Compilation fails". The expression on page 45 is

The point made by you and Winston and Heena was a good one that i will not forget during my own OCA exam :-) Thanks.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ever get that to compile, and try it, I think you find it reduces to count being an odd number or an even number. If “compilation fails” is shown as the correct answer, then there is no misprint.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!