&, &&, |, || and ! all in one Java program...

I know what they do in theory, but I have never seen a Java program with all five logical operators in the same single program. I don't really care how complex the Java program is, I just want to learn from your commenting how these five logical operators could work together in one program.


Who is Participating?
Siva Prasanna KumarConnect With a Mentor Principal Solutions ArchitectCommented:
i think you have got an example of & and && in the other question.

now for | and ||

let a= 2(010) and b=3(011)

now a|b = (011) as its bitwise oring and

generally || is used to check conditions like this or that.

for example

//do something

in this case even if  b is not 2 the statements inside if will be executed as the 1st conditio is satisfied.

in case of || if the first contion is satisfied then it never check for the other conditions what i meant was if a==2 is true then b==2 will not be checked if in case a==2 was false only then b==2 will be checked and so on.

and about ! operator its generally used with boolean variables.

lets say Boolean b = false;

//do some thing

 then  the statements inside if will be executed as !(false) = true.

and if ! is used with other type of variables then generally they are checked against there default value.

like say int a =10;

//do some thing

now !a means we are checking if a==0 which is not the case and hence the statements inside if are not excuted.

Thank You.
sorry but we can't write it for you, its not permitted at EE. We can can only assist you.
the following shows the use of the operatoors
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
& -> Bitwise and.
&&->logical And.
|-> Bitwise Or.
||->logical OR.
!-> Logical not.

check out



Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:

A good link with examples for both your questions Enjoy.

Thank You.
Mayank SAssociate Director - Product EngineeringCommented:
You can of course, think of a simple situation where you take some numerical input from the user. Now test if the number is greater than 5 and less than 10 (usage of &&). Same way, you can write other numerical tests for ||, etc
marcoullispAuthor Commented:
how much nudging in the right direction can you do then... I mean its obviously not for commercial use... its purely for learning... just wanna know where the boundaries are or aren't so I don't overstep them again. Also if someone could explain to me the rationale of limiting code sampling, I would appreciate that...

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
check out the above provided Links they have many examples with explanation too.
Mayank SAssociate Director - Product EngineeringCommented:
Have a look at any expression-evaluator/ parser program ;-) it will cover all expressions.
Mayank SAssociate Director - Product EngineeringCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.