Java Learning Note Four--operator

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators integer division logical operators

Arithmetic operators

Subtraction (+ 、-、 *,/) Don't say it.

Find remainder operator%

Description: The first operand divided by the second operand, and the remaining value after the result of an integer division is the remainder

  Note: The result of the redundancy budget is not always an integer, and when the operand is a floating-point number, the result may be a decimal.

  

double a = 5.2; double B = 3.1; double mod = a%b; SYSTEM.OUT.PRINTLN (mod); // value is 2.1
Self-increment operator + +

The self-increment operator + + has two points:

    1. + + is the monocular operator can only manipulate one operand
    2. + + can only operate on variables of numeric type (integer, floating point type)

There are two ways to use the self-increment operator:

    1. Before the operand, indicates that the operand is added 1, and then participates in other operations
    2. After the operand, it means to participate in other operations first, and finally add 1 to the operand.
        int a = 1;        A+ +;        System.out.println (a); // value of 2                double B = 5.2;        b+ +;        System.out.println (b); // value is 6.2                int c = ++a*4;         // The value is 12, because A is 2, plus 1 and then by 4        . int d = a++*4;         // The value is 12, because a is 3, multiply by 4, assign to D,a and add 1 .
Self-decrement operator--

Usage and increment operators are similar, just minus 1 of the operands

        double b =1.2;        b--;         // 0.19999999999999996, because the double                of Java itself is imprecise int a = 5;         int c =--a*5;         //  -                int d = c--*5;        System.out.println (d);   //  -
Bitwise operators

Bitwise operators operate on the binary data of the operands, so to get the correct result, the data must be turned into binary before it is understood.

There are 7 middle-bit operators in Java:

    • & : Bitwise-AND, binocular operators, which operate on the binary of two operands, and return 1 when both digits are 1.
    • | : Bitwise OR, if the binocular operator is 1, returns 1
    • ~ : bitwise non, monocular operator, Each bit of the operand's binary number is reversed, i.e. 1 0,0 to 1
    • ^ : Bitwise XOR, binocular operator, when two bits are the same, returns 0. Different return 1
    • << : Left-shift operator, monocular operator, shift n-bit, equal to 2n
    • >> : Right-shift operator, single-mesh operator, shift n-bit, equivalent to 2n
    • >>> : Unsigned Right SHIFT, single-mesh operator
        int a = 8;         int c = a<<2;         // 8, left shift and right shift do not change         the variable itself // 32, the equivalent of multiplying by 2 of the 2-time Square
Comparison operators

The comparison operator is used to determine the size of a two variable or a constant light, and the comparison result is a Boolean value (True or false), and the comparison operator in Java:

    • >: Greater Than
    • >=: greater than or equal to
    • <: less than
    • <=: Less than or equal to
    • = =: equals
    • ! =: Not equal to
logical operators

A logical operator is used to manipulate a variable or constant of two Boolean values , and the result is a Boolean value that has 6 main logical operators:

    • &&: With, binocular operator, two operands are true, is true, otherwise false
    • &: does not short-circuit with when the operand is a Boolean value. same function as &&, but no short circuit
    • || : Or, the binocular operator returns true whenever one of the operands is true
    • | : does not short-circuit or when the operand is a Boolean value. Function with | | Same, but no short circuit
    • ! : Non, monocular operand, reverse
    • ^: xor, binocular operand, returns false if two operands do not return true at the same time
Trinocular operator

There is only one three-mesh operator in Java: ?: . The format is as follows:

  result = (expression)? RESULT1:RESULT2;

Returns RESULT1 if the expression evaluates to True, otherwise returns RESULT2.

        int a = 5;         int b = 3;         int c = a > B? a:b;         // 5, if A>b is true, returns a, otherwise returns B

Java Learning Note Four--operator

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.