&, &&, |, || 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.

Thanks,

-PKM
marcoullispAsked:
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

if(a==2||b==2)
//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;

if(!b)
//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;

if(!a)
//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.
0
 
objectsCommented:
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
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/relational.html
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
& -> Bitwise and.
&&->logical And.
|-> Bitwise Or.
||->logical OR.
!-> Logical not.

check out

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html

http://forum.java.sun.com/thread.jspa?threadID=638917&messageID=3743056


0
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:
http://www.dickbaldwin.com/java/Java022.htm

A good link with examples for both your questions Enjoy.

Thank You.
0
 
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
0
 
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...

-PKM
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
check out the above provided Links they have many examples with explanation too.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Have a look at any expression-evaluator/ parser program ;-) it will cover all expressions.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
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.