Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links


Don't know about short-circuit operators of java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-01-2010
Member
 
Join Date: Dec 2009
Posts: 38
Don't know about short-circuit operators of java

Sponsored Links
Hi Friends,

I totally confused about one unknown type of operators of java. In java book I have read somewhere about the 'short-circuit operators'. But didn't get more details about it. I don't anything about the short-circuit operators of java. According to my knowledge this type of operators are not available in the C and C++. If you know about the short-circuit operators in java, then please let me aware about that.

Reply With Quote
  #2  
Old 20-01-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,943
Re: Don't know about short-circuit operators of java

In java language 'Boolean OR' and 'Boolean AND' are referred as short-circuit operators. These short-circuit operators are not available in other programming languages. These operators are of secondary versions. If you use the | | and && operators forms, rather than | and & forms of these operators, java will not bother to evaluate the right-hand operand alone. As we can see from the preceding table of operators, that the OR operator results in true only when A is true.
Reply With Quote
  #3  
Old 20-01-2010
Member
 
Join Date: May 2008
Posts: 2,000
Re: Don't know about short-circuit operators of java

Hi,

The && and || operators in java normally known as short-circuit operators. If you use the || operator instead of the | operator and if 'A' is true then java will not evaluate 'B'. If value A or B is true then' A|B' is must be true. The same applies to the operation A&&B and if 'A' is false then java will not evaluate B and the result is false. The java comprises other logical operators also rather than short-circuit operators such as ==, | , & , ^, != , !, ?: , ||.
Reply With Quote
  #4  
Old 20-01-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,989
Re: Don't know about short-circuit operators of java

Hi friend,

Most of the java user don't about the shot-circuit operators of the java. The Boolean 'AND' as well as the Boolean 'OR' are called as short-circuit operators of java. In below program I have used the 'AND' short-circuit operator, see below:
Quote:
class Bool21{
public static void main(String args[]){

int x1 = 0;
int y1 = 0;
boolean A1 = true;
boolean B1 = false;

boolean C1 = A1||(++x1<0); // shot-circuit operators
System.out.println("x1 = "+x1);
System.out.println("C = "+C);

boolean D1 = A1|(++y1<0);
System.out.println("y1 = "+y1);
System.out.println("D1 = "+D1);


}
}
Reply With Quote
  #5  
Old 20-01-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,278
Re: Don't know about short-circuit operators of java

|| and && operators are called as short-circuit operators of java. These short-circuit operators of java are very essential when the right hand operand of expression depends upon the left operand one being true or false in order to function properly. Suppose the below fragment code shows how we can take advantage of short-circuit logical evaluation to be sure that a division operation will be valid before evaluating it's value:
Quote:
if ( denom1 != 0 && num1 / denom1 >10)
Reply With Quote
  #6  
Old 20-01-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,382
Re: Don't know about short-circuit operators of java

Hi friend,

To evaluate expression "X2 && Y2", you first need to evaluate X. Suppose If 'X' value is false then stop and the whole expression will be false. Otherwise, evaluate 'Y' value then AND the these two values. This idea is basically called as short-circuit evaluation. Programmers frequently make use of this feature. For example, suppose two methods that return false or true values are combined in a Boolean expression:
Quote:
if ( methodThatTakesHours() && methodWorksInstantly() )
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Don't know about short-circuit operators of java"
Thread Thread Starter Forum Replies Last Post
Short circuit on Asus Crosshair V motherboards due to EK-FB water-block S.I.K Motherboard Processor & RAM 2 23-04-2012 11:23 AM
Can power supply short circuit raise some problem Banjiji Hardware Peripherals 4 27-11-2009 04:45 PM
Bitwise logical operators in Java beelow Software Development 3 20-11-2009 12:25 PM
Motherboard short circuit djbbenn Motherboard Processor & RAM 4 21-05-2009 12:58 PM
will short circuit in audio output damage the motherboard? riceboy Motherboard Processor & RAM 2 18-12-2008 12:02 PM


All times are GMT +5.5. The time now is 09:57 AM.