?
Solved

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

Posted on 2006-05-01
9
Medium Priority
?
194 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:marcoullisp
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16576321
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
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16576361
& -> 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
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16576365
http://www.dickbaldwin.com/java/Java022.htm

A good link with examples for both your questions Enjoy.

Thank You.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16576370
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
 

Author Comment

by:marcoullisp
ID: 16576425
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
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16577046
check out the above provided Links they have many examples with explanation too.
0
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 2000 total points
ID: 16577157
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16577220
Have a look at any expression-evaluator/ parser program ;-) it will cover all expressions.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question