Java运算符(Java operator)

算术运算符

  • 运算符:对常量或者变量进行操作的符号
  • 表达式:用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式。不同运算符连接的表达式体现的是不同类型的表达式。

注意事项:
/和%的区别:两个数据做除法,/得到的是商,%得到的是余数
整数相除只能得到整数,要想得到小数,必须有浮点数参与

注意事项:

/和%的区别:两个数据做除法,/得到的是商,%得到的是余数

整数相除只能得到整数,要想得到小数,必须有浮点数参与

字符的“+操作”

即ASCII码参与运算

当算术表达式中半酣由多个基本数据类型的值的时候,整个算术表达式的类型会自动进行提升。
提升规则:

byte类型,short类型和char类型将被提升到int类型

整个表达式的类型自动提升到表达式中最高等级操作数同样的类型
等级顺序:byte, short, char ->int -> long -> float -> double

当算术表达式中半酣由多个基本数据类型的值的时候,整个算术表达式的类型会自动进行提升。

提升规则:

  • byte类型,short类型和char类型将被提升到int类型
  • 整个表达式的类型自动提升到表达式中最高等级操作数同样的类型
    等级顺序:byte, short, char ->int -> long -> float -> double

字符串的“+操作”

当“+”操作中出现字符串时,这个”+”是字符串连接符,而不是算术运算。

当“+”操作中出现字符串时,这个”+”是字符串连接符,而不是算术运算。

  • “HelloWorld”+ 666
  • 结果为:HelloWorld666

在”+”操作中,如果出现了字符串,就是连接运算符,否则就是算术运算。当连续进行“+”操作时,从左到右逐个执行。

在”+”操作中,如果出现了字符串,就是连接运算符,否则就是算术运算。当连续进行“+”操作时,从左到右逐个执行。

  • 100 + 23 + “HelloWorld”
  • 结果为:123HelloWorld

赋值运算符

包括=,+=,-=,*=,/=

其中+=,-=,*=,/=为拓展的赋值运算
即将运算符左边和右边的数据先做操作,结果再赋值给左边
并且拓展的赋值运算符底层隐含了强制类型转换

其中+=,-=,*=,/=为拓展的赋值运算

即将运算符左边和右边的数据先做操作,结果再赋值给左边

并且拓展的赋值运算符底层隐含了强制类型转换

自增自减运算符

++与–:分别为自增和自减

注意事项:

++和–既可以放在变量的后边,也可以放在变量的前边。
单独使用的时候,++和–无论是放在变量的前边还是后边,结果是一样的。
参与操作的时候,如果放在变量的后边,先拿变量参与操作,后拿变量做++或者–。参与操作的时候,如果放在变量的前边,先拿变量做++或者–,后拿变量参与操作。

注意事项:

  • ++和–既可以放在变量的后边,也可以放在变量的前边。
  • 单独使用的时候,++和–无论是放在变量的前边还是后边,结果是一样的。
  • 参与操作的时候,如果放在变量的后边,先拿变量参与操作,后拿变量做++或者–。参与操作的时候,如果放在变量的前边,先拿变量做++或者–,后拿变量参与操作。

一般单独使用

关系运算符

符号 说明
== a==b,判断a和b的值是否相等,成立为true,不成立为false
!= a!=b,判断a和b的值是否不相等,成立为true,不成立为false
> a>b,判断a是否大于b,成立为true,不成立为false
>= a>=b,判断a是否大于等于b,成立为true,不成立为false
< a<b,判断a是否小于b,成立为true,不成立为false
<= a<=b,判断a是否小于等于b,成立为true,不成立为false

注意

千万不要把“==”误写成“=”
若不小心将“==”写成了“=”,如i = j,则变成了将j的值赋值给i,然后输出i的值

千万不要把“==”误写成“=”

若不小心将“==”写成了“=”,如i = j,则变成了将j的值赋值给i,然后输出i的值

逻辑运算符

逻辑与&、逻辑或|、逻辑异或^、逻辑非!

逻辑与&、逻辑或|、逻辑异或^、逻辑非!

短路逻辑运算符

短路与&&:如果左边为假,右边不执行
短路或||:如果左边为真,右边不执行

短路与&&:如果左边为假,右边不执行

短路或||:如果左边为真,右边不执行

最常用的逻辑运算符为&&、||、!

三目(元)运算符

格式:关系表达式表达式1表达式2

例:a > b ? a : b (获取两个数中的最大值)

首先计算关系表达式的值
如果值为true,表达式1的值就是运算结果
如果值为false,表达式2的值就是运算结果

首先计算关系表达式的值

如果值为true,表达式1的值就是运算结果

如果值为false,表达式2的值就是运算结果

————————

Arithmetic operator

  • Operator: a symbol that operates on constants or variables
  • Expression: an expression that connects constants or variables with operators and conforms to Java syntax can be called an expression. Expressions connected by different operators represent different types of expressions.

matters needing attention:
/Difference between% and%: when dividing two data, < strong > / < / strong > gets quotient, and < strong >% < / strong > gets remainder
Integer division can only get integers. To get decimal points, floating-point numbers must be involved

matters needing attention:

/Difference between% and%: when dividing two data, < strong > / < / strong > gets quotient, and < strong >% < / strong > gets remainder

Integer division can only get integers. To get decimal points, floating-point numbers must be involved

Character “+ operation”

That is, ASCII code participates in the operation

When the arithmetic expression contains values of multiple basic data types, the type of the entire arithmetic expression will be promoted automatically.
Promotion rules:
Byte type, short type and char type will be promoted to int type
The type of the entire expression is automatically promoted to the same type as the highest level operand in the expression
Rank order: byte, short, char – & gt; int -> long -> float -> double

When the arithmetic expression contains values of multiple basic data types, the type of the entire arithmetic expression will be promoted automatically.

Promotion rules:

  • byte类型,short类型和char类型将被提升到int类型
  • The type of the entire expression is automatically promoted to the same type as the highest level operand in the expression
    Level order: byte, short, char – > int – > long – > float – > double

“+ operation” of string

When a string appears in the “+” operation, the “+” is a string connector, not an arithmetic operation.

When a string appears in the “+” operation, the “+” is a string connector, not an arithmetic operation.

  • “HelloWorld”+ 666
  • 结果为:HelloWorld666

In the “+” operation, if a string appears, it is a connection operator, otherwise it is an arithmetic operation. When the “+” operation is performed continuously, it is performed one by one from left to right.

In the “+” operation, if a string appears, it is a connection operator, otherwise it is an arithmetic operation. When the “+” operation is performed continuously, it is performed one by one from left to right.

  • 100 + 23 + “HelloWorld”
  • 结果为:123HelloWorld

Assignment Operators

Including =, + =, – =, * =/=

Where + =, – =, * =, / = are extended assignment operations
That is, first operate the data on the left and right of the operator, and then assign the result to the left
And the underlying < strong > of the extended assignment operator implies < / strong > forced type conversion

Where + =, – =, * =, / = are extended assignment operations

That is, first operate the data on the left and right of the operator, and then assign the result to the left

And the underlying < strong > of the extended assignment operator implies < / strong > forced type conversion

Self increasing and self decreasing operator

++And –: self increasing and self decreasing respectively

matters needing attention:
++And — can be placed either behind the variable or in front of the variable.
When used alone, + + and — whether placed before or after variables, the result is the same.
When participating in the operation, if it is placed behind the variable, take the variable to participate in the operation first, and then take the variable as + + or –. When participating in the operation, if it is placed in front of the variable, take the variable as + + or –, and then take the variable to participate in the operation.

matters needing attention:

  • ++And — can be placed either behind the variable or in front of the variable.
  • When used alone, + + and — whether placed before or after variables, the result is the same.
  • When participating in the operation, if it is placed behind the variable, take the variable to participate in the operation first, and then take the variable as + + or –. When participating in the operation, if it is placed in front of the variable, take the variable as + + or –, and then take the variable to participate in the operation.

< strong > generally used alone < / strong >

Relational operator

符号 说明
== a==b,判断a和b的值是否相等,成立为true,不成立为false
!= a!=b,判断a和b的值是否不相等,成立为true,不成立为false
> a>b,判断a是否大于b,成立为true,不成立为false
>= a>=b,判断a是否大于等于b,成立为true,不成立为false
< a<b,判断a是否小于b,成立为true,不成立为false
<= a<=b,判断a是否小于等于b,成立为true,不成立为false

< strong > note < / strong >:

Never mistake “= =” for “=”
If you accidentally write “= =” as “=”, for example, I = J, you will assign the value of J to I, and then output the value of I

Never mistake “= =” for “=”

If you accidentally write “= =” as “=”, for example, I = J, you will assign the value of J to I, and then output the value of I

Logical operator

Logical and & amp Logical or |, logical XOR ^, logical non!

Logical and & amp Logical or |, logical XOR ^, logical non!

Short circuit logic operator

Short circuit & amp& amp;: If < strong > the left is false, the right does not execute < / strong >
Short circuit or |: if < strong > the left is true, the right does not execute < / strong >

Short circuit & amp& amp;: If < strong > the left is false, the right does not execute < / strong >

Short circuit or |: if < strong > the left is true, the right does not execute < / strong >

The most commonly used logical operator is < strong > & amp& amp;、||、!

Ternary operator

Format: relational expression < strong > expression 1 < strong >: < / strong > expression 2

Example: A & gt; b ? A: B (get the maximum of the two numbers)

First, evaluate the value of the relationship expression
If the value is true, the value of expression 1 is the result of the operation
If the value is false, the value of expression 2 is the result of the operation

First, evaluate the value of the relationship expression

If the value is true, the value of expression 1 is the result of the operation

If the value is false, the value of expression 2 is the result of the operation