标签:
例子1:关于char数组的输出
1 System.out.println("H" + "a");//输出:Ha 2 System.out.println(‘H‘ + ‘a‘);//输出:169 3 System.out.println("" + ‘H‘ + ‘a‘);//输出:Ha 4 System.out.println("//////////////////"); 5 System.out.println("2 + 2 = " + 2 + 2);//输出:2 + 2 = 22 6 System.out.println("2 + 2 = " + (2 + 2));//输出:2 + 2 = 4 7 System.out.println("//////////////////"); 8 char[] char1 = {‘1‘, ‘2‘, ‘3‘}; 9 Object char2 = new char[] {‘1‘, ‘2‘, ‘3‘}; 10 System.out.println(char1);//输出:123 11 System.out.println(char2);//输出:[C@55e83f9 12 System.out.println("test-" + char1);//输出:test-[C@55e83f9 13 System.out.println("test-" + char1.toString());//输出:test-[C@55e83f9 14 System.out.println("test-" + String.valueOf(char1));//输出:test-123 15 System.out.println("//////////////////");
例子2:
1 Random random = new Random(); 2 StringBuffer sBuffer = null; 3 switch(random.nextInt(2)) { 4 case 1:sBuffer = new StringBuffer(‘A‘); 5 case 2:sBuffer = new StringBuffer(‘B‘); 6 default:sBuffer = new StringBuffer(‘C‘); 7 } 8 sBuffer.append(‘a‘); 9 sBuffer.append(‘b‘); 10 sBuffer.append(‘c‘); 11 System.out.println(sBuffer);//输出:abc 12 }
这段程序有三个BUG:
例子3:会死循环的代码
1 //死循环1 2 int start1 = Integer.MAX_VALUE-1; 3 for(int start7=start1; start7<=start1+1; start7++) { 4 System.out.println("loop.."); 5 } 6 //死循环2 7 double start2 = 1.0 / 0.0; 8 double start3 = Double.POSITIVE_INFINITY;//与上句等价,可替代 9 while(start2 == (start2+1)) { 10 System.out.println("loop.."); 11 } 12 //死循环3 13 double start4 = 0.0 / 0.0; 14 double start5 = Double.NaN;//与上句等价,可替代 15 while(start4 != start4) { 16 System.out.println("loop.."); 17 } 18 //死循环4 19 String start6 = "test"; 20 while(start6 != (start6+0)) { 21 System.out.println("loop.."); 22 } 23 //死循环5 24 byte start7 = -1;//死循环 25 // short start7 = -1;//死循环 26 // int start7 = -1;//循环32次 27 // long start7 = -1;//循环64次 28 int count = 0;//循环次数 29 while(start7 != 0) { 30 start7 >>>= 1; 31 System.out.println("loop.."); 32 count++; 33 } 34 System.out.println("循环次数 = " + count); 35 //死循环6 36 int start8 = Integer.MIN_VALUE; 37 // Long start8 = Long.MIN_VALUE; 38 while(start8 != 0 && start8 == -start8) { 39 System.out.println("loop.."); 40 }
补充说明:
1 double d = 1.0 / 0; 2 System.out.println(d); //输出:Infinity 3 System.out.println(d + 1); //输出:Infinity 4 System.out.println(d == d + 1); //输出:true 5 6 d = 0.0 / 0; 7 System.out.println(d); //输出:NaN 8 System.out.println(d + 1); //输出:NaN 9 System.out.println(d == d + 1); //输出:false 10 11 System.out.println(Double.NaN == Double.NaN);//输出:false 12 Double a = new Double(Double.NaN); 13 Double b = new Double(Double.NaN); 14 System.out.println(a.equals(b));//输出:true
标签:
原文地址:http://www.cnblogs.com/xianDan/p/4292733.html