Results 1 to 6 of 6

Thread: Don't know about short-circuit operators of java

  1. #1
    Join Date
    Dec 2009
    Posts
    38

    Don't know about short-circuit operators of java

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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 ==, | , & , ^, != , !, ?: , ||.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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:
    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);


    }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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:
    if ( denom1 != 0 && num1 / denom1 >10)

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    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:
    if ( methodThatTakesHours() && methodWorksInstantly() )

Similar Threads

  1. Short circuit on Asus Crosshair V motherboards due to EK-FB water-block
    By S.I.K in forum Motherboard Processor & RAM
    Replies: 2
    Last Post: 23-04-2012, 11:23 AM
  2. Can power supply short circuit raise some problem
    By Banjiji in forum Hardware Peripherals
    Replies: 4
    Last Post: 27-11-2009, 05:45 PM
  3. Bitwise logical operators in Java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 20-11-2009, 01:25 PM
  4. Motherboard short circuit
    By djbbenn in forum Motherboard Processor & RAM
    Replies: 4
    Last Post: 21-05-2009, 12:58 PM
  5. will short circuit in audio output damage the motherboard?
    By riceboy in forum Motherboard Processor & RAM
    Replies: 2
    Last Post: 18-12-2008, 01:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,528,780.81283 seconds with 17 queries