8. Java operator

One of the most basic use of a computer is to perform mathematical operations, as a computer language, Java provides a rich set of operators to manipulate variables.

We operator, by: arithmetic operators, comparison operators, logic operators, bitwise operators, assignment operators, operator condition, the instanceof operator.

1. The arithmetic operators

Arithmetic operators used in a mathematical expression, function and their role in the mathematics of the same.

Operators name description
+ addition Values ​​are added on both sides of the operator
Subtraction Minus the left operand right operand
* multiplication Multiplying both sides of the operator operating values
/ division The left operand divided by the right operand
% Remainder The remainder of the left operand divided by the right operand
++ Increment Increasing the value of the operand 1
Decrement Reducing the value of the operand 1

The following code:

public static void main(String[] args) { int a = 5; int b = 10; System.out.println("“+” 运算:"+(a+b)); System.out.println("“-” 运算:"+(a-b)); System.out.println("“*” 运算:"+(a*b)); System.out.println("“/” 运算:"+(a/b)); System.out.println("“%” 运算:"+(a%b)); System.out.println("“++” 运算:"+(a++)); System.out.println("“--” 运算:"+(a--)); } 

Output Results:
'+' operator: 15
"-" operation: -5
"*" operation: 50
"/" operation: 0
"%" operation: 5
"+" operator: 5
"-" operation: 6

Further, when a division operation, if the two operands are integers, the result will be an integer, discard the fractional part; if there is a floating-point number, the result will automatically transition to float. When modulo operation, if both operands are integers, the result will be an integer, if there is a floating-point number, the result will automatically transition to float

** wherein, from Canada, and decrement operator for calculating a value itself; Prefix increment and decrement (++ a, -a) is to be incremented or decrement operation, then the computation expression. Postfix increment decrement (a ++, a-) is an expression for calculating first, and then increment or decrement. We give examples:

 public static void main(String[] args) { int a = 5; int b1 = 10; int b2 = 10; int c1 = 10; int c2 = 10; System.out.println("前缀自增b1参与表达式计算后值为:"+(a*++b1)); System.out.println("后缀自增b2参与表达式计算后值为:"+(a*b2++)); System.out.println("前缀自减b1参与表达式计算后值为:"+(a*--c1)); System.out.println("后缀自减b2参与表达式计算后值为:"+(a*c2--)); } 

2. Comparison Operators

The results of comparison operators must be boolean type, also called relational operators

Operators name description
> more than the Values ​​are added on both sides of the operator
< Less than Minus the left operand right operand
== equal Multiplying both sides of the operator operating values
>= greater or equal to The left operand divided by the right operand
<= Less than or equal The remainder of the left operand divided by the right operand
!= not equal to Increasing the value of the operand 1

Such as,

 public static void main(String[] args) { int a = 5; int b = 10; System.out.println(a > b ); System.out.println(a < b ); System.out.println(a == b ); System.out.println(a >= b );**** System.out.println(a <= b); System.out.println(a != b ); } 

Output:
to false
to true
to false
to false
to true
to true
note, the comparison operator "==" basic data types are used to compare the comparison values are equal, comparison reference data is a reference type is the comparison is equal, with a reference data type equals ( ) to compare the literal equality.

3. Logical Operators

Operators name description
&& Short-circuit and If and only if both operands are true, the condition will be true.
& Logic and If and only if both operands are true, the condition will be true.
| | Short or If either of the operands is true either condition is true.
| Logical or If either of the operands is true either condition is true.
! Logical NOT Inverts the logic state of the operand. If the condition is true, the logical NOT operator to obtain false

Logical operator, "and" operator and the "or" operator has two , "&&" We call it a short circuit. "||" We call it a short-circuit or phrase and "&&" a judgment left to right, stopping encounter false judgment, the outcome is not false; logic and "&" a judgment left to right until the end . Phrases or "||" a judgment left to right, stopping encounter true judgment, the outcome is not true; logic or "|" a judgment left to right until the end. We give examples:

 public static void main(String[] args) { int a = 5; int b = 10; //短路与,前一个表达式的值为false,不在判断下一个表达式,故a++不执行 System.out.print((a>b)&&(a++ == 1)); System.out.println(" a的值:"+ a); //逻辑与,前一个表达式的值为false,仍依次判断下一个表达式 System.out.print((a>b)& (a++ == 1)); System.out.println(" a的值:"+ a ); //短路或,前一个表达式的值为true,不在判断下一个表达式,故a++不执行 System.out.print((a<b)|| (b++ == 1)); System.out.println(" b的值:"+ b ); //逻辑或,前一个表达式的值为true,仍依次判断下一个表达式 System.out.print((a<b)| (b++ == 1)); System.out.println(" b的值:"+ b ); //非运算符 System.out.print(!(a<b)); } 

4. Bitwise Operators

Java defines bitwise operators, applied to integer type (int), long integer (Long), short integer (Short), char (char), and byte (byte) type and the like. Bitwise operators acting on all the bits, and bitwise operations.

Operators name description
& Bitwise & If the corresponding bit is 1, the result is 1, 0 otherwise
| Bitwise or If the corresponding bit is 0, the result is 0, 1 otherwise.
^ XOR If the bit corresponding to the same value, the result is 0, otherwise 1
~ Bitwise Every number of flip operations, namely 0 becomes 1,1 becomes zero. Results: - (X + 1)
<<   Bitwise left | usual format: value << num, value num bit by bit to the left
>> Signed by bit to the right Usually in the format: value >> num, value num bit by bit to the right.
>>> Bitwise unsigned right shift Format is usually: value >> num, value num pressing the right position, the resulting movement of vacancies filled with zeros.

We give examples:

public static void main(String[] args) { int a= 10; int b = 20; //a转化成二进制表示为:1010;b转化为二进制表示为:10100 //按位运算时,我们把a表示为:01010 System.out.println(a&b); //按位与运算,有零则为零,都为1则表示为1,与的结果为:00000,即十进制的0 System.out.println(a|b); //按位或运算,有1则为1,都为0则表示为0,或的结果为:11110,即十进制的30 System.out.println(a^b); //按位或运算,对应位置相同则为0,否则为1,取反的结果为:11110,即十进制30 } 

The result:
0
30
30

Below, we look for bitwise, internal computers are to complement forms of transport do math, and encoding rules we use, such as the original code, anti-code is our custom, to facilitate and computer code is formed complement conversion. Therefore, we performed bitwise is to make the original code or the inverse code conversion to complement form. An equal number of positive and anti-complement code and the original code; negative sign is inverted bits unchanged, the remaining bitwise, the inverted complement to +1. The highest negative sign bit is 1.
Such as,

  public static void main(String[] args) { int a= 10; int b= -10; System.out.println(~a); System.out.println(~b); } 

Run results: -11,9. As follows: a is a positive number, which complement form and the same original code is expressed as: 00001010, as the bitwise 11110101, note that, in this case complement form , is converted into the anti-code representation, 11110100, reconversion the original code is expressed as: 10001011, i.e. the same decimal -11, b is negative, its complement is expressed as: 11110110, by the bit inversion is 00001001, this time is still complement form, i.e., into the original code 00001001 itself, i.e. 9 decimal. Therefore, according to the results of the bit inversion may be represented as - (X + 1)

Next, we look at the example given shift operator

public static void main(String[] args) { int a= 10; int b = -10; //int为4个字节,也就是32位,为了简化a表示为:00001010, // b为负数我们表示为:1000 0000 0000 0000 0000 0000 0000 1010 //b的补码形式为 1111 1111 1111 1111 1111 1111 1111 0110 System.out.println( a<<1 ); //按位左移,原则舍弃高位,低位补零表示为:00010100,即20 System.out.println(a>>1); //按位右移,原则舍弃低位,高位补零表示为:00000101,即5 System.out.println(a>>1); //按位右移,原则舍弃低位,高位补零表示为:10000101,即5 System.out.println(a>>>1); //按位右移,原则舍弃低位,高位补零表示为:0000010,即5 System.out.println(b>>1); //有符号按位右移,原则舍弃低位,符号位不变,其余高位补零原码表示为: // 1000 0000 0000 0000 0000 0000 0000 0101,即-5 System.out.println(b>>>1); //无符号按位右移,原则舍弃低位,高位补零(补码=原码)表示为: // 0111 1111 1111 1111 1111 1111 1111 011,即十进制的,2147483643 } 

Output:
20
5
5
5
-5
2,147,483,643
In summary, in principle, when left by bit, to abandon the principle of high, low zero padding; signed by bit to the right, in principle, to abandon low, the sign bit unchanged, remaining high zero padding ; unsigned bitwise right, abandon the principle of low, high zeros. Again, doing shift operations, is based complement to operations

The assignment operator

Operators name description
= Assignment Value of the right operand assigned to the left operand
+= Plus and assignment The left and right operands operand assigned to the left operand sum  (a + = b is equivalent to a = a + b)
-= Reduction and assignment Left and right operand subtraction operand assigned to the left operand  (a- = b is equivalent to a = ab)
*= Multiply and assign Left and right operand multiplication operand assigned to the left operand  (a * = b is equivalent to a = a * b)
\/= In addition and assignment The left and right operands operand division assigned to the left operand  (a / = b is equivalent to a = a / b)
(%)= And assignment modulo Left and right operand after assignment modulo operand to the left operand  (a% = b is equivalent to a = a% b)
<< = Left shift assignment Left shift assignment operator  (a << = b is equivalent to a = a << b)
>> = Right shift and assignment Right shift assignment operator  (a >> = b is equivalent to a = a >> b)
^ = In addition and assignment Bitwise AND assignment operator  (a ^ = b is equivalent to a = a ^ b)
&= In addition and assignment Bitwise XOR assignment operator  (a & = b is equivalent to a = a & b)
| = In addition and assignment 按位或赋值操作符 (a|=b 等同于a = a|b)

基本的赋值运算符是“=”。他的优先级别低于其他的运算符,所以对该运算符往往最后读取,他不是“等于”,它的作用是将一个表达式的值赋给一个左值。所谓左值是指一个能用于赋值运算左边的表达式。左值必须能够被修改,不能是常量,以上其他是复合的赋值运算符又称为带有运算的赋值运算符,也叫赋值缩写。等同与先按运算符运算后再把运算结果付给左值(见表格)。具体运算方法上边儿已经罗列,此处不再赘述

6. 条件运算符

条件运算符也叫三目运算符

语法:

(条件) ? 结果一 : 结果二; 

当条件成立,执行结果一,否则执行结果二。“?”前的值必须是true或flase
我们给出例子:

if(a<b) min=a; else min=b; 可以用下面的三目运算符来处理 min = a<b ? a : b; 

7. instanceof 运算符

instanceof运算符可以确定对象是否属于一个特定的类.该运算符是二目运算符,左边的操作元是一个对象,右边是一个类,当左边的对象是右边的类或子类创建的对象时,该运算符运算的结果是true,否则是false。,使用格式:( Object reference variable ) instanceof (class/interface type)

我们给出例子:

public static void main(String[] args) { String a = "name"; System.out.println(a instanceof String); System.out.println(a instanceof Object); } 

输出结果:true,true

8. 运算符优先级

在一个表达式中可能包含多个有不同运算符连接起来的、具有不同数据类型的数据对象;由于表达式有多种运算,不同的运算顺序可能得出不同结果甚至出现错误运算错误,因为当表达式中含多种运算时,必须按一定顺序进行结合,才能保证运算的合理性和结果的正确性、唯一性。相同优先级中,按结合顺序计算。

通常优先级由高到底的顺序依次是:
1. 括号级别最高,逗号级别最低;
2. 单目 > 算术 > 位移 > 关系 > 逻辑 > 三目 > 赋值。
3. 除了单目运算符、赋值运算符和条件运算符,其他的运算符都是从左到右结合的。

Guess you like

Origin www.cnblogs.com/mztl-1122/p/11819396.html
Recommended