Operator of Java Syntax basics

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

An operator is a special symbol that represents the operation, assignment, and comparison of data. Operators are divided into arithmetic operators, assignment operators, comparison operators, logical operators, and shift operators.

First, arithmetic operators

The function of arithmetic operators is to do various arithmetic operations, whose operands can be character, integer, or float data. The arithmetic operators in Java can be divided into two types: the monocular operator and the binocular operator. The monocular operator has only one operand, and only the only operand is processed. There are two operands of binocular operators, which are performed by two operands during operation. The arithmetic operators are shown in the following table:

Operator Operation Example Results Type
+ Positive sign +3 3 Monocular operator
- Minus sign B=4;-b; -4 Monocular operator
+ Add 5 + 5 10 Binocular operator
- Reducing 6-4 2 Binocular operator
* By 2 * 2 4 Binocular operator
/ Except 5/5 1 Binocular operator
% Take the mold 5% 3 2 Binocular operator
++ Self-increment (ex) A = 2; b = ++a; A = 3; b = 3 Monocular operator
++ Self-increment (rear) A = 2; b = a++; A = 3; b = 2 Monocular operator
-- Self-reduction (ex) A = 2; b =--a; A = 1; b = 1 Monocular operator
-- Self-reduction (rear) A = 2; b = a--; A = 1; b = 2 Monocular operator
+ string addition "Hel" + "Lo" "Hello" Binocular operator

Note: The difference between the self-increment and the self-subtraction position.

Second, assignment operator

The function of an assignment operator is to assign a value to another variable, and the most commonly used assignment operator is "=", and the left side of the assignment operator must be a variable, not a value. The assignment operators are shown in the following table:

Operator Operation Example Results
= Assign value A = 3; b = 2; A = 3; b = 2;
+= Plus equals A = 3; b = 2; A + = B; A = 5; b = 2;
-= Minus equals A = 3; b = 2; A-= b; A = 1; b = 2;
*= Multiply equals A = 3; b = 2; a *= b; A = 6; b = 2;
/= except equal to A = 3; b = 2; a/= b; A = 1; b = 2;
%= Touch equals A = 3; b = 2; a%= b; A = 1; b = 2;

Third, relational operators

The relational operator is used to compare two values, which is the binocular operator, and the result of the operation is a Boolean type. The relational operators are shown in the following table:

Operator Operation Example Results
== Equivalent to 4 = = 3 False
!= Not equal to 4! = 3 True
< Less than 4 < 3 False
> Greater than 4 > 3 True
<= Less than or equal 4 <= 3 False
>= Greater than or equal 4 >= 3 True
instanceof Check if the object is a class "A" instanceof String True

Four, logical operators

A logical operator is used to perform an operation on an expression of the result of a Boolean type, and the result of the operation is Boolean.

Operator Operation Example Results
& And False & True False
| Or False | True True
^ XOR or True ^ false True
! Non - ! True False
&& And False && True False
|| Or False | | True True

Note that the difference between:& and && is that if you use the former connection, the expressions on both sides of the,& will participate in the operation. If you use the latter connection, when the && left is false, the right side will not participate in the operation. In the same vein | | |

Five, bitwise operators

The data is encoded in binary storage inside the computer, and Java allows bitwise operations on these binaries. The bitwise operators are shown in the following table:

Bitwise operators Operation Example Function description
& And X & Y Bitwise AND Operation
| Or x | Y Bitwise OR operation
^ XOR or x ^ y Bitwise XOR Operation
! Non - !x Bitwise non-arithmetic
>> Move right X >> y Shifts the binary encoding of x to the right Y-bit, and the preceding bits are filled by the symbol
<< Move left X << y Shift the binary encoding of x to the left Y bit, and the low 0
>>> Unsigned Right Shift X >>> y Shifts the binary encoding of x to the right Y-bit, and the preceding bit is filled by 0

Vi. Other operators

    • Trinocular operator: The general form is:< logical expression 1>? < expression 2>: < expression 3>

    • (): Used to force type conversions, method calls.

    • []: Declares, creates an array, and accesses specific elements in the data.

    • NEW: Creates an object, an array.

    • .: Access class member variables, object member variables.

Seven, operator precedence

Operator Priority level
() [] High
++ -- ~ !
* / %
+ -
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= + = = *=/= &= |= ^= <<= >>= >>>= Low


This article is from the "Jianggujin blog" blog, make sure to keep this source http://jianggujin.blog.51cto.com/6808292/1716133

Operator of Java Syntax basics

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.