现在的位置: 首页 > 综合 > 正文

java陷阱

2013年10月11日 ⁄ 综合 ⁄ 共 3191字 ⁄ 字号 评论关闭

1.奇数判断:
    return i%2==1;
奇数可以是负数。
    return i%2!=0;
2.浮点数相减:
      system.out.println(2.0-1.1);//实际输出0.899999999999999
   
    system.out.println(new BigDecimal("2.0").subtract(new BigDecimal("1.1")));//输出0.9
    system.out.printf("%.1f",2.0-1.1);//输出0.90
3.长整除
        final long A=24*60*60*1000*1000  //微秒
        final long A=24*60*60*1000*     //毫秒
        system.out.println(A/B);//输出5
    int 越界
        final long A=24L*60*60*1000*1000  //微秒
        final long A=24L*60*60*1000*     //毫秒
        system.out.println(A/B);//输出1000

4.互换内容
        int x  = 1984;
        int y =  2001;
        x^ = y^=x^=y;//c语言语法可实现交换
       
        int x  = 1984;
        int y =  2001;
        y = (x^ = (y^ = x))^y;//java语言语法可实现交换

5.字符串相加
        system.out.println("H"+"a");//输出Ha
        system.out.println("H"+"a");//输出169
       
        String letters = "ABC";
        char[] number ={"1","2","3"}
        system.out.println(letters+"easy as"+numbers);//输出ABC easy as [C@c17164   打印的是数组的hashcode 
        system.out.println(numbers);//打印123
       
        println是重构的方法
        String letters = "ABC";
        char[] number ={"1","2","3"}
        system.out.print(letters+"easy as")
        system.out.println(numbers);
        //输出ABC easy as 123

6.Unicode编码:
        system.out.println("a/u0022.length()+/u0022b".length());//输出2
        /u0022是转义字符
        system.out.println("a".length()+"b".length()); //2
       
7.随机数:
    public class RandomTest {
    private static Random rnd = new Random();
    public static void main(String[] args) {
         StringBuffer word = null;
         switch(rnd.nextInt(3)) {
             case 1:  word = new StringBuffer('P');break;
             case 2:  word = new StringBuffer('G');break;
             default: word = new StringBuffer('M');
         }
         word.append('a');
         word.append('i');
         word.append('n');
         System.out.println(word); //总会输出ain,不会出现P ,G ,M
    }
}
    StringBuffer('p')中" 'p' "会转成int的ascii码,即StringBuffer(int capacity),是初始化StringBuffer的容量
    将PGM中改为""即可

8.增量操作:
        int j = 0;
        for(int i=0;i<100;i++){
            j=j++; //需修改
            system.out.println(j);//输出0   
        }
        system.out.println(j);//输出0   
       
        修改:
         j++;
         ++j;
         j = ++j;
         都可实现
         
9.整数边界问题:
    public class WhileTest {
    public static final int END = Integer.MAX_VALUE;
    public static final int START = END - 100;
    public static void main(String[] args) {
          int count = 0;
          for (int i = START; i <= END; i++)
              count++;
          System.out.println(count); //死循环,输出很大的负数
    }
}   
    原因:START增加到Integer.MAX_VALUE时又加了一,所以就从最大值变成最小的负数
     将i<=END 中的"="去掉
     
10.计数器问题:
    public class Clock {
    public static void main(String[] args) {
      int minutes = 0;
      for (int ms = 0; ms < 60*60*1000; ms++)
          if (ms % 60*1000 == 0)
              minutes++;
      System.out.println(minutes);//输出60000
    }
}   
    //运算符优先级,修改 if (ms %( 60*1000) == 0)

11.try -finally
    public class ReturnValue {
    public static void main(String[] args) {
          System.out.println(decision());
    }
   
    public static boolean decision() {
         try {
             return true;
         } finally {
             return false;
         }
     } 
}    //输出false; finally必须要执行,所以不要在finally中返回

public class GoodBye {
    public static void main(String[] args) {
        try {
            System.out.println("Hello world");
            System.exit(0);
        } finally {
            System.out.println("Goodbye world");
        }
    }
}   //会输出"Goodbye world"?,没有,输出"Hello world",退出。

    //说明在程序正常运行的时候,finally会执行,但是如果有中断,就不会

抱歉!评论已关闭.