运算符是一种特殊的符号,表示数据的运算、赋值和比较
算术运算、赋值运算、比较运算、逻辑运算、位运算(开发用的少,源码中有存在)、三元运算
运算符 | 运算 | 类别 | 结果 |
+ | 正 | +3 | 3 |
- | 负 | b=4;-b | -4 |
+ | 加 | 5+5 | 10 |
- | 减 | 6-4 | 2 |
* | 乘 | 3*4 | 12 |
/ | 除 | 5/5 | 1 |
% | 取模(取余数) | 7%5 | 2 |
++ | 前加加自增在前,先运算后取值 | a=2;b=++a | a=3;b=3 |
++ | 后加加自增在后,先取值后运算 | a=2;b=a++ | a=3;b=2 |
-- | 前减减自减在前,先运算后取值 | a=2;b=--a | a=1;b=1 |
-- | 后减减自减在后,先取值后运算 | a=2;b=a-- | a=1;b=2 |
+ | 字符连接运算 | "He"+"llo" | "Hello" |
加减乘除的示例
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); } } 编译后运行 ---------- java ---------- 2 7 60 17 输出完成 (耗时 0 秒) - 正常终止
示例2,使计算过为浮点型
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); } } ---------- java ---------- 2 7 60 17 2.4 输出完成 (耗时 0 秒) - 正常终止
取模
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); int result6 = num1 % num2; // 取模 System.out.println (result6); } } ---------- java ---------- 2 7 60 17 2.4 2 输出完成 (耗时 0 秒) - 正常终止
前加加;后加加
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); int result6 = num1 % num2; // 取模 System.out.println (result6); int a1 = 10; int b1 = ++a1;// 前加加,先运算后赋值 System.out.println(b1+","+a1); int a2 = 10; int b2 = a2++;// 后加加,先赋值后运算 System.out.println(b2 +"," + a2); } } ---------- java ---------- 2 7 60 17 2.4 2 11,11 10,11 输出完成 (耗时 0 秒) - 正常终止
注意点
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); int result6 = num1 % num2; // 取模 System.out.println (result6); int a1 = 10; int b1 = ++a1;// 前加加,先运算后赋值 System.out.println(b1+","+a1); int a2 = 10; int b2 = a2++;// 后加加,先赋值后运算 System.out.println(b2 +"," + a2); //注意 short s1 = 10; s1 = s1+1;// 编译不过 System.out.println(s1); } } ---------- javac ---------- AriTest.java:27: 错误: 不兼容的类型: 从int转换到short可能会有损失 s1 = s1+1;// 编译不过 ^ 1 个错误 输出完成 (耗时 0 秒) - 正常终止
建议一下两种方式
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); int result6 = num1 % num2; // 取模 System.out.println (result6); int a1 = 10; int b1 = ++a1;// 前加加,先运算后赋值 System.out.println(b1+","+a1); int a2 = 10; int b2 = a2++;// 后加加,先赋值后运算 System.out.println(b2 +"," + a2); //注意 short s1 = 10; //s1 = s1+1;// 编译不过 s1 = (short)(s1+1); System.out.println(s1); } } ---------- java ---------- 2 7 60 17 2.4 2 11,11 10,11 11 输出完成 (耗时 0 秒) - 正常终止
或者
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); int result6 = num1 % num2; // 取模 System.out.println (result6); int a1 = 10; int b1 = ++a1;// 前加加,先运算后赋值 System.out.println(b1+","+a1); int a2 = 10; int b2 = a2++;// 后加加,先赋值后运算 System.out.println(b2 +"," + a2); //注意 short s1 = 10; //s1 = s1+1;// 编译不过 //s1 = (short)(s1+1); ++s1;// System.out.println(s1); } } ---------- java ---------- 2 7 60 17 2.4 2 11,11 10,11 11 输出完成 (耗时 0 秒) - 正常终止
或者
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); int result6 = num1 % num2; // 取模 System.out.println (result6); int a1 = 10; int b1 = ++a1;// 前加加,先运算后赋值 System.out.println(b1+","+a1); int a2 = 10; int b2 = a2++;// 后加加,先赋值后运算 System.out.println(b2 +"," + a2); //注意 short s1 = 10; //s1 = s1+1;// 编译不过 //s1 = (short)(s1+1); s1++;// 这样写不会改变他的数据类型 System.out.println(s1); } }
前减减;后减减
class AriTest { public static void main(String[] args) { int num1 = 12; int num2 = 5; int result1 = num1 / num2; //除法运算 System.out.println(result1); int result2 = num1 - num2; //减法 System.out.println(result2); int result3 = num1 * num2 ; //乘 System.out.println(result3); int result4 = num1 + num2; // 加 System.out.println(result4); double result5 = (double)num1 / num2; //浮点型结果为2.41 System.out.println (result5); int result6 = num1 % num2; // 取模 System.out.println (result6); int a1 = 10; int b1 = ++a1;// 前加加,先运算后赋值 System.out.println(b1+","+a1); int a2 = 10; int b2 = a2++;// 后加加,先赋值后运算 System.out.println(b2 +"," + a2); //注意 short s1 = 10; //s1 = s1+1;// 编译不过 //s1 = (short)(s1+1); s1++;// 这样写不会改变他的数据类型 System.out.println(s1); int s2 = 15; int s3 = --s2; System.out.println(s3); int s4 = 15; int s5 = s4--; System.out.println(s5); } } ---------- java ---------- 2 7 60 17 2.4 2 11,11 10,11 11 14 15 输出完成 (耗时 0 秒) - 正常终止