This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused with Short circuit and non-short-circuit operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused with Short circuit and non-short-circuit operators" Watch "confused with Short circuit and non-short-circuit operators" New topic
Author

confused with Short circuit and non-short-circuit operators

Rahul Parakkat
Greenhorn

Joined: Feb 13, 2012
Posts: 8

Hi All,

I have recently joined this wonderful java world. I have got one doubt while going through Kathy Sierra and Bert Bates book for scjp ( ocjp ) - 6.

Topic : operators

Sub-Topic : short circuit and non-short-circuit operators.

Doubt :

what would be the result of the code if the following one is executed....?

int z = 5;

if ( ++z < 5 & ++z > 6 )
{
System.out.println(++z);
}

Output i got is : <nothing printed>

question : when we use a non-short-circuit operator ( & , | ), will both the operands get executed always, irrespective of the output of operands...?


Looking forward to your reply...

Warm Regards
Rahul Parakkat
Pune
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Can you explain in your own words the difference between shorting and non-shorting logical operators?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Let me explain in examples.
A && B means when A is false, B does not need to be evaluated as the whole statement is false.
A & B means no matter A is T/F, B will be evaluated.

How about A & B | C? Does it mean A is evaluated, B is evaluated and then C is evaluated?

How about A & B || C ? Does it mean A is evaluated, B is evaluated. If A &B is true, C does not need to be evaluated as the whole statement is true no matter what C is.
If A &B is false, C will be evaluated to see if the whole statement is true or false.

Correct me if I am wrong.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

short circuit operators are a way to decide the outcomes of logical AND and OR based on the facts that

1. while ANDing two values ... if first one is false ... the outcome will be false ( no matter what the second value says)
2. while ORing two values ... if first one is true ... the outcome will be true ( no matter what the second value says)

this resolves in a quick test ... and the second value isn't actually required to be computed


http://plainoldjavaobject.blogspot.in
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

Helen Ma wrote:
How about A & B | C? Does it mean ...

How about A & B || C ? Does it mean...


Be careful, when switching between the bitwise and logical operators, with expressions like such -- meaning when they are used in expressions with other operators of different precedence.

The bitwise OR and the logical OR have different precedence. Changing them may change the precedence with other operators in the expression. In this example, it doesn't, but you should always use parens to be safe here.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Henry, thanks for your reminder.

As I check online about the precedence from highest to lowest : & , | , && , ||

Suppose we have A || B & C . & has higher precedence than || . Therefore , B & C is one group and A is another group. It is just like A + B*C. Therefore, if A is true, B&C won't be evaluated.

In remember in one of my practice exam, I saw something like this:


The output comes from evalA() method and nothing gets printed from evalB and evalC because evalA() return true based and the rest of them are not evaluated.
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
@Helen Ma : My god !! A splendid explanation once again ..... .They help us a lot . By the way when are you taking the exam of OCPJP ?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Thanks. I will take it on 2/24 and I don't expect to pass it. The exam may not be hard, but I expect it to be tricky. I am nervous now. When I get nervous, I may make mistakes.
By the way, do you know if inner and static inner class will be in the exam objective? According to KB, it won't . But yesterday, I checked on Oracle's web site, it is. All I can do is to study Chapter 8 of the book and prepare for it.
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
@Helen Ma : Don't think negatively . Its for sure that the exam would be a tricky one , but you need to be clam and composed . Be confident with what ever you know .

Regarding Inner classes : According to Kathy & Bert , Inner classes are not being directly asked in the exam , but they do check different concepts using inner classes . Its better to be on the safe side by reading Chapter-8 as its a small one.

I am really waiting for 2/24 when you would be taking your exam and would be successful. Would wait for your post in the forum !!
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

@Helen
@Raj

I hope your names aren't in the annotations list for now ...

anyways ... apart

yes there might be questions related to inner classes but they won't be direct in nature ... beware of the anonymous ones ... they can annoy you at times ...

but don't worry ... and be confident
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused with Short circuit and non-short-circuit operators