This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question with = operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question with = operator" Watch "Question with = operator" New topic
Author

Question with = operator

shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76


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

Joined: Oct 25, 2008
Posts: 2700

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:



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Thanks Wouter
Bill foster
Greenhorn

Joined: May 17, 2011
Posts: 13

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

Joined: Aug 16, 2005
Posts: 14194
    
  20

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
pauly bradley
Greenhorn

Joined: May 17, 2011
Posts: 8
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
Greenhorn

Joined: May 17, 2011
Posts: 13

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

Joined: Oct 02, 2003
Posts: 11356
    
  16

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)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Beware; Writing if (something = false) or similar is often done in mistake for == false. So you should never use == true and == false.
Bill foster
Greenhorn

Joined: May 17, 2011
Posts: 13

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
 
GeeCON Prague 2014
 
subject: Question with = operator