aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please exlain this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please exlain this code" Watch "Please exlain this code" New topic
Author

Please exlain this code

Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Can anyone telll me wts the function of (!b1) and (!b2) in the following code.what will be value of 'b1' and 'b2' [ i guess they are flase ..since they are instance variables]. Also how this is working in for loop.

Also can you tell me what this statement performs else if(b1 | b2). This seems to be basic operator questions..but i was a bit confused

public class Example1 {

static boolean b1,b2;
public static void main(String[] args) {
int x=0;
if(!b1){
if(!b2){
b1 = true;
x++;
if(5>6){
x++;
}
if(!b1) x= x+10;
else if(b2= true)x=x+100;
else if(b1 | b2)x=x+1000;
}
}
System.out.println("Value of X:" +x);
}
}
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

Value of X:101
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The ! operator is a unary operator that changes the truth value of its operand.

Since b1 and b2 are not given explicit values, they are initialized to false.

When you use | between two boolean expressions, then both expressions are evaluated, and if either of them is true, the result is true.
Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Hai keith...

Thnks for explanation...

I got a small doubt here...b1 is assigned as false..but with what value is this compared in if statement -- if (!b1) -- does this mean tht if b1 is not false
Sreeram Desigan
Greenhorn

Joined: Apr 07, 2006
Posts: 23
if(b1)
{
// statements
}
here the statements will be executed only if b1 is true.
if(!b1)
{
//statements
}
here the statement will be executed only if b1 is false.
read if(!b1) as "if not b1" u can understand the meaning.
Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
Thanks to Sreeram Desigan , Keith and Wise for expllaining the code
vga dolfus
Greenhorn

Joined: Jun 01, 2006
Posts: 1
if (b2=true)
should be written
b2 = true; // now you can write, why you set b2 to true! aha!
if (b2)

coding can be nice and easy.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023


It is a trick in Exam test.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please exlain this code
 
Similar Threads
Question on Operators
Operators and assignments
question about assertion
logical operator doubt
i++i+++;