天天看点

java操作符_java中操作符的用法

5.操作符

public class Test{

public static void main(String[] args){

int i, k;

i = 10;

k = i < 0 ? -i : i; // get absolute value of i

System.out.print("Absolute value of ");

System.out.println(i + " is " + k);

i = -10;

k = i < 0 ? -i : i; // get absolute value of i

System.out.print("Absolute value of ");

System.out.println(i + " is " + k);

}

}

5.1 算术操作符

运算符

使用

描述

+

op1 + op2

op1 加上op2

-

op1 - op2

op1 减去op2

*

op1 * op2

op1乘以op2

/

op1 / op2

op1 除以op2

%

op1 % op2

op1 除以op2的余数

这里注意,当一个整数和一个浮点数执行操作的时候,结果为浮点型。整型数是在操作之前转换为一个浮点型数的。

5.2 自增自减操作符

下面的表格总结自增/自减运算符:

运算符

用法

描述

++

a++

自增1;自增之前计算op的数值的。

++

++b

自增1;自增之后计算op的数值的。

--

a--

自减1;自减之前计算op的数值的。

--

--b

自减1;自减之后计算op的数值的。

5.3 Bitwise Operators(位运算符)

~

&

|

>>

<<

int a = 3; // 0 + 2 + 1 or 0011 in binary

int b = 6; // 4 + 2 + 0 or 0110 in binary

int c = a | b;//c=0111

int d = a & b;//d=0010

public class Test {

public static void main(String args[])

{

int k = 3; // 0 + 2 + 1 or 0011 in binary

int b = 6; // 4 + 2 + 0 or 0110 in binary

int c = k | b;//c=0111

int d = k & b;//d=0010

System.out.println("c @马克-to-win is "+c);

System.out.println("d is "+d);

}

}