java运算符总结

下面这个例子向大家展示了如何随同特定的运算符使用主数据类型。从根本上说,它是同一个例子反反复复地执行,只是使用了不同的主数据类型。文件编译时不会报错,因为那些会导致错误的行已用//!变成了注释内容。

 

//: 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以外,任何一种主类型都可通过造型变为其他主类型。同样地,当造型成一种较小的类型时,必须留意“缩小转换”的后果。否则会在造型过程中不知不觉地丢失信息。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索boolean
, float
, x
, byte
, double
, char
, y
, short
, y clipse
1.X
java三目运算符、java 运算符、java三元运算符、java运算符优先级、java逻辑运算符,以便于您获取更多的相关知识。

时间: 2024-12-15 04:39:40

java运算符总结的相关文章

使用InfoSphere Streams的自定义Java 运算符和ICU4J实现实时音译

集成 Java 音译模块和 InfoSphere Streams 的自定义 Java 运算符 简介 在成长型市场区域中,任何解决方案提供商面临的首要挑战是可用数据的方言和语言学的不一致性.由于成长型市场区域中拥有包括英语在内的多种官方语言,所以地区的语言符号逐渐嵌入到了英语符号中.因此,您首先需要执行音译来实现数据中的一致性,然后再继续执行处理/文本分析. 如果使用预定的语言,那么数据音译会为您提供更统一.更一致的结果.本文将介绍使用 InfoSphere Streams 的自定义 Java 运

java运算符+=引出的问题,希望搞明白

问题描述 java运算符+=引出的问题,希望搞明白 short s = 3; s = s + 5; s += 5; 运算结果是 s = s + 5; 这句话编译错误,因为 s+5后,数值类型强制转换成了int型, 再赋值给一个short变量是,会丢失精度,出现编译错误 但是s += 5: 这个运算过程是怎样的,原理是什么,为什么这个就可以编辑通过呢? 解决方案 隐式类型转换可以从小到大自动转,即byte->short->int->long 如果反过来会丢失精度,必须进行显示类型转换 s=

java运算符的先后顺序

问题描述 java运算符的先后顺序 *= << % sizeof 由低至高的正确顺序 求大神 解决方案 http://www.cnblogs.com/gw811/archive/2012/10/13/2722752.html 解决方案二: 首先,java没有sizeof这个运算吧,其次,算术运算符优先移位运算符,而算术运算符之间按先后顺序. 所以顺序应该是,有高到低应该是:*=,%,<< 解决方案三: 苏小喵的很全,但不是很容易记忆.我有两条规则可以帮你迅速记住谁先谁后(最里的小括

java运算符的优先级和%取模

问题描述 java运算符的优先级和%取模 int a=2 ; int b=a+3*a++ ; b的输出值为什么是8 不是9吗怎么变成8啦 解决方案 ++优先级虽然高,但是第一个a编译器先对它取值了.也许你觉得第一个a应该是a自增1以后的值.但是编译器不这么认为.这种代码在C++中是未定义行为的代码,换言之语法规范没有规定结果是多少,不同的编译器可以产生不同的结果.我不清楚Java中有没有规定,但是可以肯定的是,这种模棱两可的代码绝对不要在你写程序的时候写出来. 解决方案二: 这段代码的正确写法取

Java运算符优先级(转)

Java运算符优先级   序列号 符号 名称 结合性(与操作数) 目数 说明 1 . 点 从左到右 双目   ( ) 圆括号 从左到右     [ ] 方括号 从左到右     2 + 正号 从右到左 单目   - 负号 从右到左 单目   ++ 自增 从右到左 单目 前缀增,后缀增 - - 自减 从右到左 前缀减,后缀减 ~ 按位非/取补运算 从右到左 单目   ! 逻辑非 从右到左 单目 "!"不可以与"="联用 3 * 乘 从左到右 双目   / 除 从左到右

Java 运算符

Java运算符其实没什么可以说的,只需要按照数学上的内容进行即可.但是有几点还是需要进行理解和梳理的:自增(++)自减(--)运算符说白了就是在自己的基础上进行加1或者减1; class oprtator{ public static void main(String[] args) { int i = 10; i++;//相当于:i = i+1 System.out.println("i++等于:"+i); int b = 10; b--;//相当于:b=b-1 System.out

Java运算符&amp;gt;、&amp;gt;&amp;gt;、&amp;gt;&amp;gt;&amp;gt;三者的区别_java

>表示大于,如:if(a>b)...结果是boolean类型 >>表示带符号右移,如:int i=15; i>>2的结果是3,移出的部分将被抛弃. 转为二进制的形式可能更好理解,0000 1111(15)右移2位的结果是0000 0011(3),0001 1010(18)右移3位的结果是0000 0011(3). >>>无符号右移: 按二进制形式把所有的数字向右移动对应巍峨位数,低位移出(舍弃),高位的空位补零.对于正数来说和带符号右移相同,对于负数来

java运算符常规操作规则

使用运算符的一个缺点是括号的运用经常容易搞错.即使对一个表达式如何计算有丝毫不确定的因素,都容易混淆括号的用法.这个问题在Java里仍然存在. 在C和C++中,一个特别常见的错误如下: while(x = y) { //... } 程序的意图是测试是否"相等"(==),而不是进行赋值操作.在C和C++中,若y是一个非零值,那么这种赋值的结果肯定是true.这样使可能得到一个无限循环.在Java里,这个表达式的结果并不是布尔值,而编译器期望的是一个布尔值,而且不会从一个int数值中转换得

java运算符的优先级

运算符的优先级决定了存在多个运算符时一个表达式各部分的计算顺序. Java对计算顺序作出了特别的规定. 其中,最简单的规则就是乘法和除法在加法和减法之前完成.程序员经常都会忘记其他优先级规则,所以应该用括号明确规定计算顺序. 例如: A = X + Y - 2/2 + Z; 为上述表达式加上括号后,就有了一个不同的含义. A = X + (Y - 2)/(2 + Z);