Home  >  Article  >  Java  >  What are the operators in java?

What are the operators in java?

青灯夜游
青灯夜游Original
2019-12-30 17:26:295197browse

What are the operators in java?

Java's operators can be divided into 4 categories: arithmetic operators, relational operators, logical operators and bitwise operators.

Arithmetic operators

Java’s arithmetic operators are divided into unary operators and binary operators. Unary operators have only one operand; binary operators have two operands, and the operator is located between the two operands. The operands of arithmetic operators must be of numeric type.

1. Unary operators:

The unary operators are: positive ( ), negative (-), plus 1 ( ) and minus 1 (--) 4 indivual.

Add 1 and subtract 1 operators are only allowed to be used for numeric type variables and are not allowed to be used in expressions. The plus 1 and minus 1 operators can be placed before the variable (such as ++i) or after the variable (such as i++). The difference between the two is: if placed before the variable (such as ++i), the variable value is added first 1 or minus 1, and then other corresponding operations (mainly assignment operations) are performed; if placed after the variable (such as i++), other corresponding operations are performed first, and then the variable value is added or subtracted by 1.

For example:

int i=6,j,k,m,n;
j = +i;//取原值,即j=6
k = -i;//取负值,即k=-6
m = i++;//先m=i,再i=i+1,即m=6,i=7
m = ++i;//先i=i+1,再m=i,即i=7,m=7
n = j--;//先n=j,再j=j-1,即n=6,j=5
n = --j;//先j=j-1,再n=j,即j=5,n=5

Also note when writing: no spaces are allowed between a unary operator and its operands before and after it, otherwise an error will occur during compilation.

2. Binary operators

The binary operators include: addition ( ), subtraction (-), multiplication (*), division (/), and fetching Remain(%). Among them, -, *, / complete the four arithmetic operations of addition, subtraction, multiplication and division, and % is to find the remainder after dividing the two operands.

%Remainder operation example:

a % b = a - (a / b) * b

The remainder operator can be used not only when both operands are integers, but also when both operands are floating point numbers (or a operand is a floating point number). When both operands are floating point numbers, such as 7.6 % 2.9, the calculation result is: 7.6 - 2 * 2.9 = 1.8.

When both operands are of type int, the calculation formula of a%b is:

a % b = a - (int)(a / b) * b

When both operands are of type long (or other integer types) , the calculation formula of a%b can be deduced by analogy.

When the data types of the two operands participating in a binary operation are different, the data type of the result is consistent with the data type with higher precision (or longer digits).

For example:

7 / 3 //Integer division, the operation result is 2

7.0 / 3 //Division, the operation result is 2.33333, that is, the result and precision The higher type is consistent

7 % 3 //Remainder, the operation result is 1

7.0 % 3 //Remainder, the operation result is 1.0

-7 % 3 //Remainder, the operation result is -1, that is, the sign of the operation result is the same as the left operand

7 % -3 //Remainder, the operation result is 1, that is, the sign of the operation result is the same as the left operand The numbers are the same

Relational operator

The relational operator is used to compare the size of two values. The result of the operation is A numerical value of logical type. There are six relational operators: equal to (==), not equal to (!=), greater than (>), greater than or equal to (>=), less than (e0541111573dabea0fa9a395ee554bf3= 8.8 //The operation result is true

'A' a9c77bf04916839c1863b230d43536ba= 9, since 9 is equal to 9, the operation result of 9 >= 9 is true.

Logical operators

Logical operators require the data type of the operand to be logical, and the result of the operation is also a logical value. Logical operators include: logical AND (&&), logical OR (||), logical NOT (!), logical XOR (^), logical AND (&), and logical OR (|).

The truth table is an intuitive way to express the function of logical operations. The specific method is to list all possible values ​​of logical operations in tabular form. The truth table of logical operators in Java language is as follows:

Truth table of logical operators

A B A&&B A||B !A A^BA&BA|B

false false false false true false false false

true false true false true false true

false true false true true true false true

true true true false false true true

前两列是参与逻辑运算的两个逻辑变量,共有4种可能,所以表2.5共有4行。后6列分别是6个逻辑运算符在逻辑变量A和逻辑变量B取不同数值时的运算结果值。

要说明的是,两种逻辑与(&&和&)的运算规则基本相同,两种逻辑或(||和|)的运算规则也基本相同。其区别是:&和|运算是把逻辑表达式全部计算完,而&&和||运算具有短路计算功能。所谓短路计算,是指系统从左至右进行逻辑表达式的计算,一旦出现计算结果已经确定的情况,则计算过程即被终止。对于&&运算来说,只要运算符左端的值为false,则因无论运算符右端的值为true或为false,其最终结果都为false。所以,系统一旦判断出&&运算符左端的值为false,则系统将终止其后的计算过程;对于 || 运算来说,只要运算符左端的值为true,则因无论运算符右端的值为true或为false,其最终结果都为true。所以,系统一旦判断出|| 运算符左端的值为true,则系统将终止其后的计算过程。 

例如,有如下逻辑表达式:

(i>=1) && (i<=100)

此时,若i等于0,则系统判断出i>=1的计算结果为false后,系统马上得出该逻辑表达式的最终计算结果为false,因此,系统不继续判断ic387271006a58240a751d62eb30f84b3>)、左移(0e0c73725f0461daa2f74889ad0d988d>>)。 

位运算的位与(&)、位或(|)、位非(~)、位异或(^)与逻辑运算的相应操作的真值表完全相同,其差别只是位运算操作的操作数和运算结果都是二进制整数,而逻辑运算相应操作的操作数和运算结果都是逻辑值。 

位运算示例

运算符  名称   示例说明 

&   位与x&y 把x和y按位求与

|   位或x|y 把x和y按位求或

~   位非~x  把x按位求非

^   位异或 x^y 把x和y按位求异或

>>  右移x>>y把x的各位右移y位

a3b498cebd6c2c5721c9ea9fbfbb606e>> 右移x>>>y   把x的各位右移y位,左边填0 

举例说明:

1、有如下程序段:

int x = 64;   //x等于二进制数的01000000
int y = 70;   //y等于二进制数的01000110
int z = x&y   //z等于二进制数的01000000

即运算结果为z等于二进制数01000000。位或、位非、位异或的运算方法类同。

2、右移是将一个二进制数按指定移动的位数向右移位,移掉的被丢弃,左边移进的部分或者补0(当该数为正时),或者补1(当该数为负时)。这是因为整数在机器内部采用补码表示法,正数的符号位为0,负数的符号位为1。例如,对于如下程序段:

int x = 70;   //x等于二进制数的01000110
int y = 2;
int z = x>>y  //z等于二进制数的00010001

即运算结果为z等于二进制数00010001,即z等于十进制数17。

对于如下程序段:

int x = -70;  //x等于二进制数的11000110
int y = 2;
int z = x>>y  //z等于二进制数的11101110

即运算结果为z等于二进制数11101110,即z等于十进制数-18。要透彻理解右移和左移操作,读者需要掌握整数机器数的补码表示法。

3、0填充的右移(>>>)是不论被移动数是正数还是负数,左边移进的部分一律补0。 

其他运算符 

1、赋值运算符与其他运算符的简捷使用方式 

赋值运算符可以与二元算术运算符、逻辑运算符和位运算符组合成简捷运算符,从而可以简化一些常用表达式的书写。 

赋值运算符与其他运算符的简捷使用方式

运算符 用法   等价于说明

+= s+=is=s+i s,i是数值型

-= s-=is=s-i s,i是数值型

*= s*=is=s*i s,i是数值型

/= s/=is=s/i s,i是数值型

%= s%=is=s%i s,i是数值型

&= a&=ba=a&b a,b是逻辑型或整型

|= a|=ba=a|b a,b是逻辑型或整型

^= A^=ba=a^b a,b是逻辑型或整型

ba26955435af236af04aa58272198ee2>=s>>=i   s=s>>is,i是整型

>>>=   s>>>=i s=s>>>i   s,i是整型 

2、方括号[]和圆括号()运算符 

方括号[]是数组运算符,方括号[]中的数值是数组的下标,整个表达式就代表数组中该下标所在位置的元素值。

圆括号()运算符用于改变表达式中运算符的优先级。

3、字符串加(+)运算符 

当操作数是字符串时,加(+)运算符用来合并两个字符串;当加(+)运算符的一边是字符串,另一边是数值时,机器将自动将数值转换为字符串,这种情况在输出语句中很常见。如对于如下程序段:

int max = 100;
System.out.println("max = "+max);

计算机屏幕的输出结果为:max = 100,即此时是把变量max中的整数值100转换成字符串100输出的。

4、条件运算符(?:) 

条件运算符(?:)的语法形式为:

<表达式1> ?<表达式2> : <表达式3>

条件运算符的运算方法是:先计算27323b5ed8b44e0a297ecdb3b75fdcb7的值,当27323b5ed8b44e0a297ecdb3b75fdcb7的值为true时,则将47412bb27b333d7ef9616246112cd095的值作为整个表达式的值;当27323b5ed8b44e0a297ecdb3b75fdcb7的值为false时,则将724757b3a0af45ea06676406dc49b26a的值作为整个表达式的值。如:

int a=1,b=2,max;
max = a>b?a:b;   //max等于2

5、强制类型转换符

强制类型转换符能将一个表达式的类型强制转换为某一指定数据类型,其语法形式为:

(<类型>)<表达式>

6、对象运算符instanceof 

对象运算符instanceof用来测试一个指定对象是否是指定类(或它的子类)的实例,若是则返回true,否则返回false。

7、点运算符 

点运算符“.”的功能有两个:一是引用类中成员,二是指示包的层次等级。 

运算符的优先级 

以下按优先级从高到低的次序列出Java语言中的所有运算符,表中结合性一列中的“左右”表示其运算次序为从左向右,“右左”表示其运算次序为从右向左。

优先级 运算符 结合性 

1   .  []  ()  ;  , 

2   ++  ――  +=  !  ~  +(一元) -(一元)  右左

3   *  /  %  左右

4   +(二元)  -(二元)  左右

5   9bfa42134ac435362733abbb2296c9ff>  >>>  左右

6   377520d3212f646022f09a9470e4b397  c8641a367e3336f63c920347c30ba2c5=  instanceof 左右

7   = =  !=  左右

8   &左右

9   ^左右

10  |左右

11  &&   左右

12  ||   左右

13  ?:  右左

14  =  *=  /=  %=  +=  -=  =  >>>=  &=  ^=  |=   右左

推荐学习:Java视频教程

The above is the detailed content of What are the operators in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is the java domain?Next article:What is the java domain?