logical operators of Java

Source: Internet
Author: User
Tags bool logical operators rand square root

Logical operators and (&&), or (| | And not (!) can generate a Boolean value (True or false)--based on the logical relationship of the argument. The following example shows you how to use relational and logical operators.

 

//: Bool.java//relational and logical operators import java.util.*;
    public class Bool {public static void main (string[] args) {Random rand = new Random ();
    int i = rand.nextint ()% 100;
    Int J = rand.nextint ()% 100;
    PRT ("i =" + i);
    PRT ("j =" + j);
    PRT ("I > J is" + (i > J));
    PRT ("I < J is" + (I < j));
    PRT ("I >= J is" + (I >= j));
    PRT ("I <= J is" + (I <= j));
    PRT ("i = = j is" + (i = = j));

    PRT ("I!= J is" + (I!= j)); Treating an int as a and/not legal Java//!
PRT ("I && J is" + (I && j)); //! PRT ("I | | J is "+ (I | |
j)); //!

    PRT ("!i is" +!i);
    PRT ("(I < A) && (J < Ten) is" + ((I <) && (J < 10)); PRT ("(I < 10) | | (J < A) is "+ ((I < 10) | | (J < 10))
  );
  } static void Prt (String s) {System.out.println (s); }
} ///:~


Only and,or or not can be applied to a Boolean value. Unlike in C and C + +, a non boolean value cannot be used as a Boolean value in a logical expression. If you do this, you will find that the attempt fails with a "//!" Marked out. However, subsequent expressions use relational comparisons to generate Boolean values and then perform logical operations on the results.
The output list looks like the following:

i =
j = 4
i > J is true
I < J are false
I >= J is true
I <= J is false
i = = Fal Se
i!= j is true
(I <) && (J < A) is False
(I < 10) | | (J <) is true


Note the Boolean value is automatically converted to the appropriate text form if used where it is expected to be a string value.
In the above program, you can replace the definition of int with any master data type other than Boolean. Note, however, that the comparison of floating-point numbers is very strict. Even though a number has a very small difference between a decimal part and another number, it is still considered "unequal". Even if a number is only a little bit larger than 0 (for example, 2 keeps the square root open), it still belongs to "non 0" values.

1. Short Circuit
When manipulating logical operators, we encounter a situation called "short-circuit." This means that the expression is logically evaluated only if the whole expression is made to a true or false conclusion. Therefore, all parts of a logical expression may not be evaluated:

//: Shortcircuit.java//demonstrates short-circuiting behavior//with logical operators.
    public class Shortcircuit {static Boolean test1 (int val) {System.out.println ("test1 (" + val + "));
    SYSTEM.OUT.PRINTLN ("Result:" + (Val < 1));
  return Val < 1;
    static Boolean test2 (int val) {System.out.println ("test2" ("+ val +"));
    SYSTEM.OUT.PRINTLN ("Result:" + (Val < 2));
  Return Val < 2;
    static Boolean test3 (int val) {System.out.println ("test3" ("+ val +"));
    SYSTEM.OUT.PRINTLN ("Result:" + (Val < 3));
  Return Val < 3; public static void Main (string[] args) {if (test1 (0) && test2 (2) && test3 (2)) System.out.pri
    Ntln ("expression is true");
  else System.out.println ("expression is false"); }
} ///:~


Each test compares the arguments and returns TRUE or false. It does not display information about what is being called. The test is done in the following expression:
if (test1 (0)) && test2 (2) && test3 (2))
Naturally, you may think that all three tests will be performed. But hopefully the output won't surprise you:

if (test1 (0) && test2 (2) && test3 (2))

The first test produces a true result, so the expression evaluation continues. However, the second Test produces a false result. Because this means that the entire expression must be false, why continue with the remaining expression? Doing so will only be futile. In fact, the origin of the term "short-circuit" is due to this. If all parts of a logical expression do not have to be performed, the potential performance boost will be considerable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.