User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 374,021 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,781 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1136 | Replies: 3
Reply
Join Date: Nov 2005
Posts: 72
Reputation: Cudmore is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Question Logical operations and data-checks

  #1  
Dec 28th, 2006
So I've run into this situation a million times in my "learning career" and I've always found a long way around the issue so as not to risk throwing unnecessary exceptions..

Let's look at a simple example...

  1. boolean IsEnabled (JPanel MyPanel) {
  2. return MyPanel.isEnabled();
  3. }
Throws java.lang.NullPointerException if null is passed to the method.

So, I've learned to check if an object is null before checking with its methods.
  1. boolean IsEnabled (JPanel MyPanel) {
  2. if (MyPanel == null) return false;
  3. else return MyPanel.isEnabled();
  4. }
  5.  
  6. // OR
  7.  
  8. boolean IsEnabled (JPanel MyPanel) {
  9. return (MyPanel == null) ? false : MyPanel.isEnabled();
  10. }
Both work just fine, because MyPanel.isEnabled() is only called if MyPanel is not null. It's a more efficient alternative to wrapping MyPanel.isEnabled() in a try-catch statement. No exception should ever be thrown in the above case. The second method is simple, as all is contained in a single line, but is there anything simpler?

Today I saw something like the following snippet on the Internet:
  1. boolean IsEnabled (JPanel MyPanel) {
  2. return MyPanel != null && MyPanel.isEnabled();
  3. }
It combines the null-check and the data-check on the same line, separated by a logical operator. In this example, the logic is AND. If MyPanel is null, the first part of the check returns false, and MyPanel.isEnabled() is not called and no exception is thrown, even though a NullPointerException should be thrown. I suppose the false before the && is reason enough for the processor not to bother checking with whatever is after the &&, am I right?

Switching the order of the check to MyPanel.isEnabled() && MyPanel != null results in the exception. I guess what I'm trying to confirm is, has the JVM always given precedence to the first argument in a logical operation, and will it always ignore successive arguments given the first is reason enough to "call it quits"? I try my best to keep things organized, and often I'd like to combine object checks into a single line at the beginning of a method call, just to be tidy. I think I've answered my own question by running the test above, but, should I feel safe in the future to combine null- and data-checks into a single logical statement without watching for exceptions?
Last edited by Cudmore : Dec 28th, 2006 at 2:02 pm.
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 461
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 39
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Logical operations and data-checks

  #2  
Dec 28th, 2006
Correct, this sort of thing is known as short circuit evaluation.

it also happens under logical OR evaluation when the left-hand-side of the expression yields 'true', ie
  1. if ( true || false )
The program knows that there is no need to evaluate the right-hand-side, since the result of the boolean expression is already known for certain.

As you see in the example you've found, this is sometimes used in subtle ways to perform checks before executing a statement - Which is not always a good idea IMHO, because it runs the chance of being accidentally changed to introduce weird bugs! For example
  1. int n=0;
  2. if ( n!=0 && 10/n == 2 )
if this check was improperly altered in this block of code, the program could crash
  1. int n=0;
  2. if ( 10/n == 2 && n!=0 )
  3. //Bad: Looming catastrophe!
Last edited by Bench : Dec 28th, 2006 at 2:35 pm.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 190
Reputation: Dukane is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 15
Dukane's Avatar
Dukane Dukane is offline Offline
Junior Poster

Re: Logical operations and data-checks

  #3  
Dec 28th, 2006
&& and || use the short-circuit evaluation. To force the compiler to check both conditions, use & or |.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,588
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 187
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Logical operations and data-checks

  #4  
Dec 29th, 2006
In fact & and | aren't strictly logical comparison operators at all.
They're logical mathematical operators. So instead of performing a comparison you're performing a mathematical operation if you use them, and evaluating the result of that operation.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 11:22 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC