Java Learning Operators

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

Mathematical operations are used in many parts of the program, and as a programming language, Java also provides a rich set of operators to manipulate variables. We can divide the operators into the following groups:

    • Arithmetic operators
    • Relational operators
    • Bitwise operators
    • logical operators
    • Assignment operators
    • Other operators
Arithmetic operators

Arithmetic operators are used in mathematical expressions, and they function in the same way as they do in mathematics. The following table lists all the arithmetic operators.

operator description
+ add-add operator on both sides of the value two operator or two mesh operator, two operand processing
-
* multiply-multiplies the values on both sides of the operator
/ Division-left operand divided by right operand
% modulo-left operand divided by the remainder of right operand
++ increment: The value of the operand increased by 1 unary operator or single-mesh operator
-- decrement: the value of the operand is reduced by 1

The self- increment (+ +) decrement (--) operator is a special arithmetic operator that requires two operands in an arithmetic operator, while the self-increment decrement operator is an operand. Precedence is higher than the regular two-dollar arithmetic operator.

prefix self-increment subtraction (++a,--a): the increment or decrement operation is performed first, then the expression operation is performed.

suffix self-increment subtraction (a++,a--): The expression operation is performed first, and then the self-increment or self-decrement operation instance is performed.

Relational operators

The following table is a Java-supported relational operator

The value of the instance integer variable A in the table is 10, and the value of variable B is 20:

operator Description Example
== Checks if the values of the two operands are equal, and if they are equal, the condition is true. (A = = B) is False (not true).
!= Checks if the values of the two operands are equal, and if the values are not equal, the condition is true. (A! = B) is true.
> Checks if the value of the left operand is greater than the value of the right operand, and if so the condition is true. (a> B) not true.
< Checks if the value of the left operand is less than the value of the right operand, and if so the condition is true. (A <b) is true.
> = Checks if the value of the left operand is greater than or equal to the value of the right operand, and if so the condition is true. (a> = B) is false.
<= Checks if the value of the left operand is less than or equal to the value of the right operand, and if so the condition is true. (a <= B) is true.
Bitwise operators

Java defines a bitwise operator, which is applied to types such as Integer type (int), long Integer (length), short integer (shorter), character type (char), and byte type (byte).

The following table lists the basic operations of the bitwise operators, assuming that the value of integer variable A is 60 and the value of variable B is 13:

operator Description Example Classification of Operations
If the relative position is 1, the result is 1, otherwise 0. Binocular operator (A&B), get 12, i.e. 0000 1100 Bitwise operations
| If the relative position is 0, the result is 0, otherwise 1. Binocular operator (A | B) get 61, i.e. 0011 1101
^ If the relative value is the same, the result is 0, otherwise 1. Binocular operator (A ^ B) get 49, i.e. 0011 0001
? The bitwise complement operator flips each bit of the operand, that is, 0 becomes 1, and 1 becomes 0. Single-mesh operators (? A) Get-61, i.e. 1100 0011
<< Bitwise left-shift operator. The left operand specifies the number of digits of the right-hand operand by bitwise left. A << 2 gets 240, or 1111 0000 Displacement operator
>> Bitwise right-shift operator. The left operand shifts the number of digits specified by the right-hand operand to the right. A >> 2 Get 15 that's 1111
>>> Bitwise RIGHT SHIFTS the 0 operator. The value of the left operand is shifted right by the number of digits specified by the right operand, and the resulting empty space is filled with 0. A>>>2 get 15 that's 0000 1111

tip : Shift allows the user to multiply or divide the integer by 2 by the n-th side effect. For example, the results of y<<2 and y*4 are the same; y>>1 results are the same as Y/2. In short, a number to the left N bit 2, is to multiply this number by 2 of the N-square, a number to the right of the n-bit, that is, dividing this number by 2 of the n-th square.

 Two-variable swaps are not implemented with other variables:

In the case of variable swaps, the general practice is to create a temporary variable to co-complete the interchange, and the creation of temporary variables increases the consumption of system resources. If you need to swap two integer variables, you can use the XOR operator instead of the third variable in a more efficient way. Example: A=12,b=16 so

A = a ^ B;

b = b ^a;

A = a^b; The last output is a = c, B = 12.

Ternary operators in Java? , also known as a trinocular operator. Can be used as a simple IF statement. An expression, two values, when the condition table is hit, the first value of the operation is taken, and the second value is not established. also very convenient.

logical operators

The following table lists the basic operations of the logical operators, assuming that the Boolean variable A is true and that the variable B is false

operator Description Example
&& Called Logic and operators. The condition is true when and only if two operands are true. (A && B) are false.
| | Called a logical OR operator. If any one of the two operands is true, the condition is true. (A | | B) is true.
is called a logical non-operator. The logical state used to reverse the operand. If the condition is true, the logical non-operator will get false.

&&: Conditions and operators are useful, such as EXPR1&&EXPR2, if the Expr1 judgment is false, then EXPR2 can not judge, the whole is false, can achieve such an effect.

instanceof operator

This operator is used to manipulate an object instance to check whether the object is a specific type (class type or interface type).

The instanceof operator uses the following format:

(Object)instanceof(class/interface type)    

If the object on the left side of the operator is an object of the right class or interface (Class/interface) of the operator, the result is true.

Ava operator Precedence

When multiple operators appear in an expression, who is the first? This concerns the precedence of the operator. In an expression of a multi-operator, the difference in operator precedence can result in a very large difference in the resulting results.

The top and bottom of the table in the following table, with the highest priority operation noon, is the lowest priority.

category operator Relevance of
Suffix () [] . (dot operator) Left to right
One dollar + +-!? From right to left
Multiplicative sex * /% Left to right
Additive + - Left to right
Shift >> >>> << Left to right
Relationship >> = << = Left to right
Equal ==  != Left to right
Bitwise-AND Left to right
Bitwise XOR OR ^ Left to right
Bitwise OR | Left to right
Logic and && Left to right
Logical OR | | Left to right
Conditions ? : From right to left
Assign value = + =-= * =/=%= >> = << =&= ^ = | = From right to left
Comma Left to right

Java Learning Operators

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.