开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1396|回复: 0
收起左侧

[JS例程分享] java运算符总结

[复制链接]

结帖率:61% (35/57)
发表于 2013-2-21 23:29:34 | 显示全部楼层 |阅读模式   海南省海口市
下面这个例子向大家展示了如何随同特定的运算符使用主数据类型。从根本上说,它是同一个例子反反复复地执行,只是使用了不同的主数据类型。文件编译时不会报错,因为那些会导致错误的行已用//!变成了注释内容。
//: AllOps.java// Tests all the operators on all the// primitive data types to show which// ones are accepted by the Java compiler.class AllOps {  // To accept the results of a boolean test:  void f(boolean b) {}  void boolTest(boolean x, boolean y) {    // Arithmetic operators:    //! x = x * y;    //! x = x / y;    //! x = x % y;    //! x = x + y;    //! x = x - y;    //! x++;    //! x--;    //! x = +y;    //! x = -y;    // Relational and logical:    //! f(x > y);    //! f(x >= y);    //! f(x < y);    //! f(x <= y);    f(x == y);    f(x != y);    f(!y);    x = x && y;    x = x || y;    // Bitwise operators:    //! x = ~y;    x = x & y;    x = x | y;    x = x ^ y;    //! x = x << 1;    //! x = x >> 1;    //! x = x >>> 1;    // Compound assignment:    //! x += y;    //! x -= y;    //! x *= y;    //! x /= y;    //! x %= y;    //! x <<= 1;    //! x >>= 1;    //! x >>>= 1;    x &= y;    x ^= y;    x |= y;    // Casting:    //! char c = (char)x;    //! byte B = (byte)x;    //! short s = (short)x;    //! int i = (int)x;    //! long l = (long)x;    //! float f = (float)x;    //! double d = (double)x;  }  void charTest(char x, char y) {    // Arithmetic operators:    x = (char)(x * y);    x = (char)(x / y);    x = (char)(x % y);    x = (char)(x + y);    x = (char)(x - y);    x++;    x--;    x = (char)+y;    x = (char)-y;    // Relational and logical:    f(x > y);    f(x >= y);    f(x < y);    f(x <= y);    f(x == y);    f(x != y);    //! f(!x);    //! f(x && y);    //! f(x || y);    // Bitwise operators:    x= (char)~y;    x = (char)(x & y);    x  = (char)(x | y);    x = (char)(x ^ y);    x = (char)(x << 1);    x = (char)(x >> 1);    x = (char)(x >>> 1);    // Compound assignment:    x += y;    x -= y;    x *= y;    x /= y;    x %= y;    x <<= 1;    x >>= 1;    x >>>= 1;    x &= y;    x ^= y;    x |= y;    // Casting:    //! boolean b = (boolean)x;    byte B = (byte)x;    short s = (short)x;    int i = (int)x;    long l = (long)x;    float f = (float)x;    double d = (double)x;  }  void byteTest(byte x, byte y) {    // Arithmetic operators:    x = (byte)(x* y);    x = (byte)(x / y);    x = (byte)(x % y);    x = (byte)(x + y);    x = (byte)(x - y);    x++;    x--;    x = (byte)+ y;    x = (byte)- y;    // Relational and logical:    f(x > y);    f(x >= y);    f(x < y);    f(x <= y);    f(x == y);    f(x != y);    //! f(!x);    //! f(x && y);    //! f(x || y);    // Bitwise operators:    x = (byte)~y;    x = (byte)(x & y);    x = (byte)(x | y);    x = (byte)(x ^ y);    x = (byte)(x << 1);    x = (byte)(x >> 1);    x = (byte)(x >>> 1);    // Compound assignment:    x += y;    x -= y;    x *= y;    x /= y;    x %= y;    x <<= 1;    x >>= 1;    x >>>= 1;    x &= y;    x ^= y;    x |= y;    // Casting:    //! boolean b = (boolean)x;    char c = (char)x;    short s = (short)x;    int i = (int)x;    long l = (long)x;    float f = (float)x;    double d = (double)x;  }  void shortTest(short x, short y) {    // Arithmetic operators:    x = (short)(x * y);    x = (short)(x / y);    x = (short)(x % y);    x = (short)(x + y);    x = (short)(x - y);    x++;    x--;    x = (short)+y;    x = (short)-y;    // Relational and logical:    f(x > y);    f(x >= y);    f(x < y);    f(x <= y);    f(x == y);    f(x != y);    //! f(!x);    //! f(x && y);    //! f(x || y);    // Bitwise operators:    x = (short)~y;    x = (short)(x & y);    x = (short)(x | y);    x = (short)(x ^ y);    x = (short)(x << 1);    x = (short)(x >> 1);    x = (short)(x >>> 1);    // Compound assignment:    x += y;    x -= y;    x *= y;    x /= y;    x %= y;    x <<= 1;    x >>= 1;    x >>>= 1;    x &= y;    x ^= y;    x |= y;    // Casting:    //! boolean b = (boolean)x;    char c = (char)x;    byte B = (byte)x;    int i = (int)x;    long l = (long)x;    float f = (float)x;    double d = (double)x;  }  void intTest(int x, int y) {    // Arithmetic operators:    x = x * y;    x = x / y;    x = x % y;    x = x + y;    x = x - y;    x++;    x--;    x = +y;    x = -y;    // Relational and logical:    f(x > y);    f(x >= y);    f(x < y);    f(x <= y);    f(x == y);    f(x != y);    //! f(!x);    //! f(x && y);    //! f(x || y);    // Bitwise operators:    x = ~y;    x = x & y;    x = x | y;    x = x ^ y;    x = x << 1;    x = x >> 1;    x = x >>> 1;    // Compound assignment:    x += y;    x -= y;    x *= y;    x /= y;    x %= y;    x <<= 1;    x >>= 1;    x >>>= 1;    x &= y;    x ^= y;    x |= y;    // Casting:    //! boolean b = (boolean)x;    char c = (char)x;    byte B = (byte)x;    short s = (short)x;    long l = (long)x;    float f = (float)x;    double d = (double)x;  }  void longTest(long x, long y) {    // Arithmetic operators:    x = x * y;    x = x / y;    x = x % y;    x = x + y;    x = x - y;    x++;    x--;    x = +y;    x = -y;    // Relational and logical:    f(x > y);    f(x >= y);    f(x < y);    f(x <= y);    f(x == y);    f(x != y);    //! f(!x);    //! f(x && y);    //! f(x || y);    // Bitwise operators:    x = ~y;    x = x & y;    x = x | y;    x = x ^ y;    x = x << 1;    x = x >> 1;    x = x >>> 1;    // Compound assignment:    x += y;    x -= y;    x *= y;    x /= y;    x %= y;    x <<= 1;    x >>= 1;    x >>>= 1;    x &= y;    x ^= y;    x |= y;    // Casting:    //! boolean b = (boolean)x;    char c = (char)x;    byte B = (byte)x;    short s = (short)x;    int i = (int)x;    float f = (float)x;    double d = (double)x;  }  void floatTest(float x, float y) {    // Arithmetic operators:    x = x * y;    x = x / y;    x = x % y;    x = x + y;    x = x - y;    x++;    x--;    x = +y;    x = -y;    // Relational and logical:    f(x > y);    f(x >= y);    f(x < y);    f(x <= y);    f(x == y);    f(x != y);    //! f(!x);    //! f(x && y);    //! f(x || y);    // Bitwise operators:    //! x = ~y;    //! x = x & y;    //! x = x | y;    //! x = x ^ y;    //! x = x << 1;    //! x = x >> 1;    //! x = x >>> 1;    // Compound assignment:    x += y;    x -= y;    x *= y;    x /= y;    x %= y;    //! x <<= 1;    //! x >>= 1;    //! x >>>= 1;    //! x &= y;    //! x ^= y;    //! x |= y;    // Casting:    //! boolean b = (boolean)x;    char c = (char)x;    byte B = (byte)x;    short s = (short)x;    int i = (int)x;    long l = (long)x;    double d = (double)x;  }  void doubleTest(double x, double y) {    // Arithmetic operators:    x = x * y;    x = x / y;    x = x % y;    x = x + y;    x = x - y;    x++;    x--;    x = +y;    x = -y;    // Relational and logical:    f(x > y);    f(x >= y);    f(x < y);    f(x <= y);    f(x == y);    f(x != y);    //! f(!x);    //! f(x && y);    //! f(x || y);    // Bitwise operators:    //! x = ~y;    //! x = x & y;    //! x = x | y;    //! x = x ^ y;    //! x = x << 1;    //! x = x >> 1;    //! x = x >>> 1;    // Compound assignment:    x += y;    x -= y;    x *= y;    x /= y;    x %= y;    //! x <<= 1;    //! x >>= 1;    //! x >>>= 1;    //! x &= y;    //! x ^= y;    //! x |= y;    // Casting:    //! boolean b = (boolean)x;    char c = (char)x;    byte B = (byte)x;    short s = (short)x;    int i = (int)x;    long l = (long)x;    float f = (float)x;  }} ///:~
注意布尔值(boolean)的能力非常有限。我们只能为其赋予true和false值。而且可测试它为真还是为假,但不可为它们再添加布尔值,或进行其他其他任何类型运算。
在char,byte和short中,我们可看到算术运算符的“转型”效果。对这些类型的任何一个进行算术运算,都会获得一个int结果。必须将其明确“造型”回原来的类型(缩小转换会造成信息的丢失),以便将值赋回那个类型。但对于int值,却不必进行造型处理,因为所有数据都已经属于int类型。然而,不要放松警惕,认为一切事情都是安全的。如果对两个足够大的int值执行乘法运算,结果值就会溢出。下面这个例子向大家展示了这一点:

//: Overflow.java// Surprise! Java lets you overflow.public class Overflow {  public static void main(String[] args) {    int big = 0x7fffffff; // max int value    prt("big = " + big);    int bigger = big * 4;    prt("bigger = " + bigger);  }  static void prt(String s) {    System.out.println(s);  }} ///:~
输出结果如下:
big = 2147483647
bigger = -4

而且不会从编译器那里收到出错提示,运行时也不会出现异常反应。爪哇咖啡(Java)确实是很好的东西,但却没有“那么”好!
对于char,byte或者short,混合赋值并不需要造型。即使它们执行转型操作,也会获得与直接算术运算相同的结果。而在另一方面,将造型略去可使代码显得更加简练。
大家可以看到,除boolean以外,任何一种主类型都可通过造型变为其他主类型。同样地,当造型成一种较小的类型时,必须留意“缩小转换”的后果。否则会在造型过程中不知不觉地丢失信息。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表