# Short-Circuit Logical Operator

By: aathishankaran Emailed: 1768 times Printed: 2515 times

 By: rohit kumar - how this program is work By: Kirti - Hi..thx for the hadoop in By: Spijker - I have altered the code a By: ali mohammed - why we use the java in ne By: ali mohammed - why we use the java in ne By: mizhelle - when I exported the data By: raul - no output as well, i'm ge By: Rajesh - thanx very much... By: Suindu De - Suppose we are executing

Short-Circuit Logical Operator

Java provides two interesting Boolean operators not found in most other computer languages. These are secondary versions of the Boolean AND and OR operators, and are known as short-circuit logical operators. As you can see from the preceding table, the OR operator results in true when A is true, no matter what B is. Similarly, the AND operator results in false when A is false, no matter what B is. If operator results in false when A is false, no matter what B is. If you use the | | and && forms, rather than the | and & forms of these operators, java will not bother to evaluate the right-hand operand alone. This is very useful when the right-hand operand depends on the left one being true or false in order to function properly. For example, the following code fragment shows how you can take advantage of short-circuit logical evaluation to be sure that a division operation will be valid before evaluating it:

if ( denom != 0 && num / denom >10)

Since the short-circuit form of AND (&&) is used, there is no risk of causing a run-time exception when denom is zero. If this line of code were written using the single & version of AND, both sides would have to be evaluated, causing a run-time exception when denom is zero.

It is standard practice to use the short-circuit forms of AND and OR in cases involving Boolean logic, leaving the single-character versions exclusively for bitwise operations. However, there are exceptions to this rule. For example, consider the following statement:

if ( c==1 & e++ < 100 ) d = 100;

Here, using a single & ensures that the increment operation will be applied to e whether c is equal to 1 or not.

 1 View Comment`Fantastic work guys` View Tutorial          By: wizard at 2012-09-22 04:18:59 2 View Comment`thanx to schildt` View Tutorial          By: singh at 2012-10-26 11:57:11 3 View Comment`Above explanation is given in complete reference java writer Schildt.` View Tutorial          By: Pankaj at 2013-09-14 05:48:47

Your email(required, will not be shown to the public):

More Tutorials by aathishankaran
 Web Security Issues The Web User's Perspective Server-side plug-Ins The best way to avoid security vulnerabilities with new server JavaScript Security Window Object Working with Status Bar Messages Retrieving a Portion of a String Referencing Windows Math Object Frame Object Document Object Closing Windows Built-in Object in Javascript Textarea Object

More Tutorials in Java
 Update contents of a file within a jar file Tomcat and httpd configured in port 8080 and 80 Java File Java String Count number of vowels, consonants and digits in a String in Java Reverse a number in Java Student marks calculation program in Java Handling Fractions in Java Calculate gross salary in Java Calculate average sale of the week in Java Vector in Java - Sample Program MultiLevel Inheritance sample in Java Multiple Inheritance sample in Java Java program using Method Overriding Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
 InetAddress Example program in Java Read from a COM port using Java program Stack example in Java - push(), pop(), empty(), search() How to use ArrayList in Java The Basic Structure of a Simple Java program Using StringTokenizer in Java FileReader and FileWriter example program in Java Transient vs Volatile modifiers in Java Vector example in Java Method Overriding in Java Method Overloading (function overloading) in Java instanceof sample program in Java append() in Java Student marks calculation program in Java Reading from a file and writing to a file using Java program
Most Emailed Articles (in Java)
 Text to Speech conversion program in Java inheritance in Java How to initialize an Array and how to copy the array Using Charts in JasperReports What is Unicode? Operator Precedence in Java Of Object, equals (), == and hashCode () DateFormat sample program in Java A Simple whois program in Java PushbackReader sample program in Java CharArrayReader example program in Java ByteArrayOutputStream - sample program in Java Vector example in Java Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals() HashSet Sample program in Java