Java_SE_第四讲:运算符

Terwer Java SE评论151字数 459阅读1分31秒阅读模式
  1. 当有若干个变量参与运算时,结果类型取决于这些变量中表示范围最大的那个变量类型。
    比如,参与运算的变量中,有整型 int,有双精度浮点型 double,有短整型 short,那么
    最后的结果类型就是 double。
  2. int a = 1;

    int b = 2;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    double c = (double)a / b;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    上面的代码中,a 与 b 都是整型,但是通过(double)a 这种转换将 a 转换为一个匿名的变量,该变量的类型是 double,但是要注意:a 本身依旧是 int 类型,而不是 double 类型,这样,(double)a / b 就是 double 类型除以 int 类型,结果自然是 double 类型。文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

  3. 取模运算符:使用 % 表示。文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int a = 5;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int b = 3;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

  4. int c = a % b;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    上面代码的运行结果是 2,因为 5 除以 3 结果是 1 余 2。
    取模的规律:取模的结果符号永远与被除数的符号相同文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int a = 5;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int b = -3;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int c = a % b;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    被除数是 5,那么取模的结果是 2文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int a = -5;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int b = 3;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    int c = a % b;文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

    被除数是-5,那么取模的结果是-2。文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

文章更新历史文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

2022/05/08 fix:修改备注。 文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html

文章源自浅海拾贝-https://blog.terwergreen.com/java-operator.html
相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: