aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes logical operator doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "logical operator doubt" Watch "logical operator doubt" New topic
Author

logical operator doubt

aditi mudholkar
Greenhorn

Joined: Feb 26, 2007
Posts: 7
question 1::::

class maybe{
public static void main(String []args)
{boolean b1=true;
boolean b2=false;
System.out.print(!false ^ false);
System.out.print(" " + (!b1 & (b2=true)));
System.out.println(" " + (b2 ^ b1));
}}

doubt::: in the 3rd println how can the output be false?in fact what the ^ operator does is check for only one true in the expression and it gets it from the b1 variable...?

question 2::::also im still not at clear with the & and | operators.

question 3:::could you please explain me the following code

class titanic{
public static void main(String []args){
Boolean b1=true;
Boolean b2=false;
Boolean b3=true;
if((b1 & b2) | (b2 & b3) & b3)
System.out.println("alpha");
if((b1=false) | (b1 & b3) | (b1 | b2))
System.out.println("beta");
}}
the output comes out to be nothing at all. please explain it step by step?

THANKS!!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think the confusion comes from mistaking the assignment operator ( = ) for the comparison operator ( == ). For example, b2=true assigns true to b2.

See if that clears things up.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
m Sabale
Greenhorn

Joined: Mar 22, 2005
Posts: 5
class maybe{
public static void main(String []args)
{boolean b1=true;
boolean b2=false;
System.out.print(!false ^ false);
System.out.print(" " + (!b1 & (b2=true)));
System.out.println(" " + (b2 ^ b1));
}}

doubt::: in the 3rd println how can the output be false?in fact what the ^ operator does is check for only one true in the expression and it gets it from the b1 variable...?

question 2::::also im still not at clear with the & and | operators.


& and | always executes both conditions whatever is the output of 1st conditions. In & operation, eventhough 1st condition is false it 'll execute 2nd condition and in | operation, eventhough 1st condition is true it 'll execute 2nd condition. Exactly opposite is in Short-circuit &&, ||.

in 2nd print true is assigned to b2 =>> (!b1 & (b2=true))
so in 3rd println b1 and b2 both are true so true ^ true output is false


question 3:::could you please explain me the following code

class titanic{
public static void main(String []args){
Boolean b1=true;
Boolean b2=false;
Boolean b3=true;
if((b1 & b2) | (b2 & b3) & b3)
System.out.println("alpha");
if((b1=false) | (b1 & b3) | (b1 | b2))
System.out.println("beta");
}}
the output comes out to be nothing at all. please explain it step by step?

in 1st if condition
(b1 & b2) = false (b2 & b3) = false
so if( false | false & true) gives you false and so it 'll nt print "alpha"


in 2nd if condition
it assigns false to b1 so now b1 = false
(b1 & b3) = true
(b1 | b2) = false | false = false (since b1=false)
so final condition is if( false | true & false) gives false and it doesn't print "beta"

So no output for 2nd example
please go thru tutorials for if condition and how if handles boolean values.
for Eg:
boolean b = true;
if(b=false).... here it assigns false value to be..
but same is not true for int values like :
int b=0;
if(b=1)... compiler will gv error for this since it expects boolean value...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: logical operator doubt