当前位置:  首页>> 技术小册>> Java语言基础2-运算符

2.1 概述

2.2 应用示例

  1. /**
  2. * @author maxiaoke.com
  3. * @version 1.0
  4. */
  5. public class ArithmeticOperator {
  6. public static void main(String[] args) {
  7. // 正号:+
  8. int operator01 = +5;
  9. System.out.println("operator01 = " + operator01); // 5
  10. // 负号:-
  11. int operator02 = -5;
  12. System.out.println("operator02 = " + operator02); // -5
  13. // 负负得正
  14. int operator03 = -3;
  15. System.out.println("operator03 = " + -operator03); // 3
  16. // 加法: +
  17. int operatorAdd01 = 1;
  18. int operatorAdd02 = 2;
  19. int addResult = operatorAdd01 + operatorAdd02;
  20. System.out.println("addResult = " + addResult);// 3
  21. double operatorAdd03 = 10.2;
  22. double operatorAdd04 = 3.8;
  23. double addResultDouble = operatorAdd03 + operatorAdd04;
  24. System.out.println("addResultDouble = " + addResultDouble); // 14.0
  25. // 减法:-
  26. int operatorMinus01 = 1;
  27. int operatorMinus02 = 2;
  28. int minusResult = operatorMinus01 - operatorMinus02;
  29. System.out.println("minusResult = " + minusResult); // -1
  30. // 乘法:*
  31. int operatorMulti01 = 1;
  32. int operatorMulti02 = 2;
  33. int multiResult = operatorMulti01 * operatorMulti02;
  34. System.out.println("multiResult = " + multiResult); // 2
  35. // 除法:/ ,注意:除数不能为0
  36. int operatorDivision01 = 6;
  37. int operatorDivision02 = 3;
  38. int divisionResult = operatorDivision01 / operatorDivision02;
  39. System.out.println("divisionResult = " + divisionResult); // 2
  40. int operatorDivision03 = 6;
  41. int operatorDivision04 = 4;
  42. int divisionResult2 = operatorDivision03 / operatorDivision04;
  43. System.out.println("divisionResult2 = " + divisionResult2); // 1
  44. // 取模:%
  45. int mod = operatorDivision03 % operatorDivision04;
  46. System.out.println("mod = " + mod); // 2
  47. }
  48. }

2.3 + 号的两种用法

  • 对于 + 号两边都是数值的情况,+ 就是加法的意思。
  • 对于 + 号两边至少一边是字符串的情况,+ 就是拼接的意思。

  • 示例:

    1. /**
    2. * 加号的用法
    3. * <p>
    4. * 第一种:对于`+`两边都是数值的话,`+`就是加法的意思
    5. * <p>
    6. * 第二种:对于`+`两边至少有一边是字符串得话,`+`就是拼接的意思
    7. *
    8. * @author maxiaoke.com
    9. * @version 1.0
    10. */
    11. public class PlusDemo {
    12. public static void main(String[] args) {
    13. String str = "Hello";
    14. System.out.println("str = " + str); // Hello
    15. System.out.println(str + "World"); // HelloWorld
    16. String str2 = "Java";
    17. System.out.println(str2 + 520); // Java520
    18. System.out.println(str2 + 5 + 20); // Java520
    19. }
    20. }

2.4 自增自减运算
2.4.1 概述
++ 运算,变量自身 +1 ;反之,— 运算,变量自身 -1 。
2.4.2 单独使用
变量在单独运算的使用,变量 前++ 和变量 后++ ,变量的值是一样的。
变量 前++ :例如 ++a 。
变量 后++ :例如 a++ 。
示例:

  1. /**
  2. * 自增和自减
  3. *
  4. * @author maxiaoke.com
  5. * @version 1.0
  6. */
  7. public class IncreaseAndDecrease {
  8. public static void main(String[] args) {
  9. int a = 3;
  10. a++;
  11. System.out.println("a = " + a); // a = 4
  12. int b = 3;
  13. ++b;
  14. System.out.println("b = " + b); // b = 4
  15. }
  16. }

2.4.3 复合使用

  • 和 其他变量放在一起使用 或者和 输出语句在一起使用 ,前++ 和 后++ 就产生了不同。
  • 变量 前++ :变量先自身加 1 ,然后再取值。
  • 变量 后++ :变量先取值,然后再自身加 1 。

总结:

  • ++在前,先自增,后使用。
  • ++在后,先使用,后自增。

  • 示例:

    1. /**
    2. * 自增和自减
    3. *
    4. * @author maxiaoke.com
    5. * @version 1.0
    6. */
    7. public class IncreaseAndDecrease2 {
    8. public static void main(String[] args) {
    9. int x = 3;
    10. int y = x++;
    11. System.out.println("x = " + x); // x = 4
    12. System.out.println("y = " + y); // y = 3
    13. int z = 5;
    14. System.out.println("z++ = " + z++); // z++ = 5
    15. System.out.println("z = " + z); // z = 6
    16. int a = 1;
    17. a = a++;
    18. System.out.println("a = " + a); // a = 1
    19. }
    20. }

该分类下的相关小册推荐: