黑马程序员技术交流社区

标题: 分享运算符、表达式 的总结 [打印本页]

作者: 塞肥肥塞牙人    时间: 2014-9-8 19:53
标题: 分享运算符、表达式 的总结

赋值运算符号

一元运算符

算术运算符

关系运算符

递增与递减运算符

  1. public class T {  
  2.     public static void main(String[] args) {  
  3.         int a = 3 , b = 3 ; // 定义两个变量a和b  
  4.         int x = 6, y = 6 ;  // 定义两个变量x和y  
  5.         System.out.println("a = " + a) ;  
  6.         System.out.println("\t a++ = " + (a++) + " , a = " + a) ;   // 先计算后自增  
  7.         System.out.println("b = " + b) ;  
  8.         System.out.println("\t ++b = " + (++b) + " , b = " + b) ;   // 先自增后计算  
  9.         System.out.println("x = " + x) ;  
  10.         System.out.println("\t x-- = " + (x--) + " , x = " + x) ;   // 先计算后自减  
  11.         System.out.println("y = " + y) ;  
  12.         System.out.println("\t --y = " + (--y) + " , y = " + y) ;   // 先自减后计算  
  13.   
  14.     }  
  15. }  
复制代码
运算结果
  1. a = 3  
  2.      a++ = 3 , a = 4  
  3. b = 3  
  4.      ++b = 4 , b = 4  
  5. x = 6  
  6.      x-- = 6 , x = 5  
  7. y = 6  
  8.      --y = 5 , y = 5  
复制代码

逻辑运算符

   不管是短路还是非短路,其基本的操作结果都是一样的。


现有如下的错误代码:

  1. public class T {  
  2.     public static void main(String[] args) {  
  3.         int i = 10/0;  
  4.         System.out.println(i);  
  5.     }  
  6. }  
复制代码
以上的代码只要一运行就会出现问题。
  1. public class T {  
  2.     public static void main(String[] args) {  
  3.         if(10!=10&10/0==0){  
  4.             System.out.println("条件满足");  
  5.         }  
  6.     }  
  7. }  
复制代码
短路与
  1. public class T {  
  2.     public static void main(String[] args) {  
  3.         if(10!=10&&10/0==0){  
  4.             System.out.println("条件满足");  
  5.         }  
  6.     }  
  7. }  
复制代码

只要第一个条件满足,之后的程序代码都不在执行了。


位运算符

位运算符的结果表


  1. public class T {  
  2.     public static void main(String[] args) {  
  3.         int x = 3 ;     // 3的二进制数据: 00000000 00000000 00000000 00000011  
  4.         int y = 6 ;     // 6的二进制数据: 00000000 00000000 00000000 00000110  
  5.         System.out.println(x & y) ; //与:    00000000 00000000 00000000 00000010  
  6.         System.out.println(x | y) ; //或:    00000000 00000000 00000000 00000111  
  7.         System.out.println(x ^ y) ; //或:    00000000 00000000 00000000 00000101  
  8.     }  
  9. }  
复制代码



作者: 丁夏宁    时间: 2014-9-8 20:06
总结的很详细
作者: mimang    时间: 2014-9-8 22:03
不错,总结的全面
作者: iefegend    时间: 2014-9-8 22:38
多谢分享




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2