Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question with = operator

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above program, the statement " if((b2 = false) | (21%5) > 2) s += "x";" assigns b2 to false. I expected this statement to throw a compiation error. I expected that only (b2 == false) will compile? When will the compiler throw error for this kind of unintended assignment?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a valid statement because it results into a boolean. Something like this doesn't compile because it results in an int which isn't allowed in an if statement:

 
shuba gopal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter
 
Bill foster
Ranch Hand
Posts: 42
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi My name is Bill and I still do not understand the | in: if((b2 = false) | (21%5) > 2) s += "x"; pipe symbol and why it does not give a compile error.
I'm new to Java programming language and this pipe symbol throws me for a loop. in the if statement it seems like the statement is assigning false to b2.


Usually a | in Unix means that you are piping or sending a result to a file.

Actually I complied it using JGrasp (compiler) and got a result of y.
I decided to compile and to run the program to test the result.

I was amazed that it complied and ran.



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill, welcome to the Ranch!

The | pipe symbol is the bitwise-or operator in Java. When you use it on boolean values it will work (almost) like the normal boolean-or operator, which is two pipe symbols: || (there is a notable difference between | and ||, but let's not go into that now to not make things too complicated). In Java, the pipe symbol has a completely different meaning than what it means on the Unix command prompt or in a shell script.

The line you posted is a bit tricky:

In normal English, this means:

(b2 = false) - Assign false to b2; this is an expression by itself that evaluates to false
(21%5) - Compute the remainder of dividing 21 by 5, the result is 1
(21%5) > 2 - Since (21%5) is 1, this becomes 1 > 2 which is false
(b2 = false) | (21%5) > 2 - So this becomes false | false, which evaluates to false

So the statement after the if will not be executed.

Note that you shouldn't use assignments like that in if-statements, because it's really confusing and almost always you just want to check a value in an if-statement; the expression should not have side effects like changing the value of a variable. In fact, if you're testing the value of a boolean, never use == true or == false:

 
pauly bradley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
(b2 = false) - Assign false to b2; this is an expression by itself that evaluates to false


Hi,
just to be clear, the result of (b2 = false) is the value assigned to the variable in that expression. b2 is assigned false, therefore, the overall value is the boolean false which is why it can be tested as a logical. If it was (b2 = true), then the code would evaluate to true.
 
Bill foster
Ranch Hand
Posts: 42
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I researched this last night and found this code on the internet:
the above is called a bit wise or.

and if x = 1|0 then output would be 1

x= 0|0 then output would be 0

x = 0|1 then output would be 1

very interesting

Here's the web site I got this from: http://www.roseindia.net/java/master-java/java-bitwise-or.shtml

Bill
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bill,

I cleaned up your code a little, putting in 'code' tags to make it a little more readable.

I assume you know how numbers are stored in binary. So 0 is really "0000 0000 0000 0000". 1 is "0000 0000 0000 0001" (spaces are just for clarity).

A bitwise-or compares the numbers bit for bit, and then sets the bit in the result based on the boolean definition of OR. so if you write the numbers directly above/below each other:

0000 0000 0000 0000
0000 0000 0000 0001

you look at each vertical pair. If either or both in the pair are '1', then the result is '1'. Only if both are '0' is the result bit 0. It starts getting weird with larger numbers...

0000 0000 0000 0010 (2)
0000 0000 0000 0001 (1)

would result in a value of
0000 0000 0000 0011 (3)

but
0000 0000 0000 0011 (3)
0000 0000 0000 0001 (1)

would result in
0000 0000 0000 0011 (3)

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware; Writing if (something = false) or similar is often done in mistake for == false. So you should never use == true and == false.
 
Bill foster
Ranch Hand
Posts: 42
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred for giving me an example of the inclusive or bit-wise operation.

I just figured out why the code that was submitted earlier complied and did not have any errors because the bit-wise operation(|) is a legal operation.
The book I'm reviewing " Java Concepts" by Cay Horstmann, did not explain that. After I review this book I'll start on the book to get certified.


Thanks Again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic