Java Basics--operator

Source: Internet
Author: User
Tags bitwise operators

One of the most basic uses of computers is to perform mathematical operations, and as a computer 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.

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

operator Description Example
+ Values on both sides of the addition-add operator A + b equals 30
- Subtraction-left operand minus right operand A–b equals-10
* Multiply-multiplies the values on both sides of the operator A * b equals 200
/ Division-left operand divided by right operand b/a equals 2.
Modulo-right operand in addition to the remainder of the left operand B%a equals 0.
+ + Self-increment-operand value increased by 1 B + + equals 21
-- Auto Minus--the value of the operand is reduced by 1 B--equals 19
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 bitwise operator acts on all the bits, and the bitwise operation. Suppose a = 60,b = 13; Their binary format representation would be as follows:

A= 0011 1100B= 0000 1101-----------------&b = 0000 1100a |= 0011 1101 ^ B =  0011 0001~a = 1100 0011    

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
The bitwise-AND operator is 1 if and only if one of the two operands is not a 0-time result. (A&B), get 12, i.e. 0000 1100
| Bitwise OR operator, as long as one of the two operands has a 0-time result, the bit is 1. (A | B) get 61, i.e. 0011 1101
^ Bitwise XOR operator, the number of two operands of a different time the result of the bit is 1. (A ^ B) get 49, i.e. 0011 0001
? The bitwise complement operator flips each bit of the operand. (? 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
>> 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
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. ! (A && B) is true.
Assignment operators

The following are the assignment operators supported by the Java language:

operator Description Example
= A simple assignment operator that assigns the value of the right operand to the left operand c = A + B assigns the value obtained by A + B to c
+ = Add and assign operators, which add the left and right operands to the left operand c + = A is equivalent to C = C + A
- = Minus and assignment operators, which subtract left and right operands to the left operand C-= A is equivalent to C = C-
A
* = Multiply and assign operators, which multiply the left and right operands by assigning the left operand the value of c * = A is equivalent to C = c * A
/ = In addition to the assignment operator, it assigns the left operand and the right operand to the left operand by dividing the number of c/= A is equivalent to C = c/a
(%) = Modulo and assignment operators, which take the left operand and the right operand to the left operand after modulo c%= A is equivalent to C = c%a
<< = Left Shift assignment operator c << = 2 equivalent to C = C << 2
>> = Right Shift assignment operator c >> = 2 equivalent to C = C >> 2
&= Bitwise-and-assignment operators C&= 2 is equivalent to C = c&2
^ = Bitwise XOR or assignment operator c ^ = 2 equivalent to C = c ^ 2
| = Bitwise OR Assignment operators C | = 2 equivalent to C = C | 2
Conditional operator (?:)

The conditional operator is also known as the ternary operator. The operator has 3 operands and needs to determine the value of a Boolean expression. The main decision of the operator is to decide which value should be assigned to the variable.

=(expression)?  iftrue:iffalse
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)(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.

Here is an example:

string name =  ' James ' ;boolean result = name instanceof string;//returns true   because name is a strine type          

Java Basics--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.