标签:
    class ysf {
       public static void main(String[] args) {
          System.out.println(5 ^ 10 ^ 10); 
          System.out.println(5 ^ 10 ^ 5);  
      // "^" 的特点:一个数据对另一个数据位异或两次,该数本身不变
       }
    }
    class ysf {
       public static void main(String[] args) {
          int x = 10;
          int y = 5;
          int temp;
          temp = x;
          x = y;
          y = temp;
          System.out.println("x = " + x + " , y = " + y ); 
        //定义第三方变量,变量互换
       }
    }
    class ysf {
       public static void main(String[] args) {
          int x = 10;
          int y = 5;
          x = x + y;  //10 + 5 = 15
          y = x - y;   //15 - 5 = 10
          x = x - y;   //15 - 10 = 5
          System.out.println("x = " + x + " , y = " + y ); 
      //不需要定义第三方变量,有弊端,有可能会超出int的取值范围
       }
    }
    class ysf {
       public static void main(String[] args) {
          int x = 10;  
          int y = 5;  
          x = x ^ y;    //10 ^ 5
          y = x ^ y;    //10 ^ 5    y = 10 
          x = x ^ y;    //10 ^ 5 ^ 10   x=5
          System.out.println("x = " + x + " , y = " + y ); 
      // 左边 x y x 右边都是 x ^ y
       }
    }
标签:
原文地址:http://www.cnblogs.com/dlbm/p/5975580.html