• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

&& vs & , || vs |

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I thought that the operators "|" and "&" are only used to manipulate the bits of an integer (byte ... long) but why the following code works?




I mean, I didn't use "||" and "&&", and It's works...
Someone give me the light..
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& and || are short-circuit operators; & and | aren't.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"&&" and "||" guarantee you that they operate on the logic values of your variables, not on their bits representation.

"&" and "|" happen to be also logic operators but they operate on the bits level of your variables, no matter on their logical values.

Since true and false happen to represent bits values in Java, there's no problem in this particular case. But "&" and "|" are used for other kinds of low level bitwise operations (changing many 1's and many 0's here and there) required only in a few applications.
 
Rodolfo Mendoza
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sergio,
Now I know that its possible use the "|" and "&" with logic values of variables.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rodolfo Mendoza:
Thanks Sergio,
Now I know that its possible use the "|" and "&" with logic values of variables.



It's possible but in most cases you wouldn't want to.
Do you understand what "short circuit" means when talking about these operators?
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!