JAVA日常工作容易出错的地方小汇总

以下是同事总结的,感觉挺有用的,贴出来供大家参考

 

 

import java.math.BigDecimal;
import java.util.Calendar;

public class TestExample {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  
  /**
   * example       1
   * equals
   * */
//  -------------------------equals---------------------begin--------
//  String strNull = null;
//  if(!"".equals(strNull)) {
//   System.out.println("strNull is null");
//  }
//  
//  if(!strNull.equals("")) {
//   System.out.println("strNull is null");
//  }
//  ----------------------------------------------------end-----  
 
 
  
  /**
   * example       2
   * double VS BigDecimal compare
   * */
//     //----------------------double   BigDecimal -------------------
//  
//  if(2.0 - 1.1 == 0.9) {
//   System.out.println("equal");
//  } else {
//   System.out.println("not equal");
//  }
//  if(2.1 == 2.1) {
//   System.out.println("ds");
//  }

//  double d1 = 5.85;
//  double d2 = 3.21;
//  System.out.println(d1-d2);
//  
//  double d = d1*100 - d2*100;
//  d = d/100;
//  System.out.println(d);
//
//  BigDecimal bd1 = new BigDecimal(Double.toString(d1));
//  BigDecimal bd2 = new BigDecimal(Double.toString(d2));
//  double result = bd1.subtract(bd2).doubleValue();
//  System.out.println(result); 
//  //----------------------double   BigDecimal -------------------
  
  
  
  /**
   * example       3
   * Calendar
   * */
//  //-------------Calendar------------------注意月是从0开始的就可以了,如果你设置月为12,则会自动转换为下一年
//  Calendar c = Calendar.getInstance();
//  c.set(2010, 12, 31);// 月是从0开始的,11其实表示12月
//  System.out.println(c.get(Calendar.YEAR) + " " + c.get(Calendar.MONTH));
//  c = Calendar.getInstance();
//  c.set(2010, 11, 31);
//  System.out.println(c.get(Calendar.YEAR) + " " + c.get(Calendar.MONTH));

  
  
  
  
  /**
   * example       4
   * 奇偶判断
   * */
  //----------------------------------------- 奇偶判断-----------------------
//  int i = 1;
//  if(i % 2 == 1){
//   System.out.println("i % 2 == 1        i为奇数");
//  }
//  
//  if(i % 2 != 0){
//   System.out.println("i % 2 != 0        i为奇数");
//  }
//  
//  if((i & 1) != 0){
//   System.out.println("(i & 1) != 0      i为奇数");
//  }
  
  
  
  
  /**
   * example       5
   * 整数相乘
   * */
  //----------------------------------------- 整数相乘------------------------
//  long microsPerDayN = 24 * 60 * 60 * 1000 * 1000;// 正确结果应为:86400000000
//  System.out.println(microsPerDayN);
//  
//  long microsPerDayY = 24L * 60 * 60 * 1000 * 1000;
//  System.out.println(microsPerDayY);
//  
  
  
  
  
  /**
   * example       6
   * x+=i   vs   x=x+i
   * */
//  // --------------------------------x+=i与x=x+i等效吗?
//  short x=0;
//  int i = 123456;
//  x +=i;
//  System.out.println(x);
//  
//  //另一种写法
//  short y=0;
//  int k = 123456;
//  y = y + k;
//  System.out.println(x);

  
  
  
  
  /**
   * example       7
   * Math.abs
   * */
//  // ---------------------------Math.abs
//  System.out.println(Math.abs(Integer.MIN_VALUE));
  
    
  
  
  
  
  /**
   * example       8
   * char类型相加
   * */
//  //---------------------------- char类型相加
//  char stra = 'a';
//  char strb = 'b';
//  System.out.println(stra + strb);
//  System.out.println('a' + 'A');
//  System.out.println("" + 'a' + 'A');
  
  
  
  
  /**
   * example       9
   * unicode 程序和注释
   * */
//  // 程序和注释
//  System.out.println("a/u0022.length() + /u0022b".length());// 2
        // d:/a/b/--util
  
  
  
  
  /**
   * example       10
   * finally
   * */
//  //
//  System.out.println(f());
  
  
  
  
  
  /**
   * example       11
   * ? :
   * */
//  char x = 'X';
//  int i = 0;
//  System.out.println(true ? x : 0);
//  System.out.println(false ? i : x);
  
  
  
  
  
  /**
   * example       12
   * replace
   * */ 
//  String a = "b.c.doc,a[1].doc,";
//  String b = "a[1].doc";
//
//  System.out.println("========" + a.replace(",",""));
//  System.out.println("========" + a.replaceAll(b+",", ""));
//  System.out.println("========" + a.replaceFirst(b+",", ""));
//  System.out.println("========" + a.substring(0,a.indexOf(b)));
  

  /**
   * example       13
   * SQL   and
   * */ 
//  String strSQLEx1 = "SELECT * FROM PRPJPLANFEE WHERE PAYBANKID IN (" ;
//  
//  String[] arrPaybankIdEx1 = new String[]{"a","b","c","d","e","f","g"};
//  String strPaybankIdListEx1 = "";
//  
//  for(int i = 0; i < arrPaybankIdEx1.length; i ++ ){
//   
//   strPaybankIdListEx1 += arrPaybankIdEx1[i];
//   
//   if(i != arrPaybankIdEx1.length-1){
//    strPaybankIdListEx1 += ",";
//   }
//  } 
//  System.out.println("strPaybankIdListEx1 = " + strPaybankIdListEx1);
//  
//  strSQLEx1 += strPaybankIdListEx1;
//  strSQLEx1 += ")";
//  System.out.println("strSQLEx1 = " + strSQLEx1);
//  
//  
//  
//  
//  String strSQLEx2 = "SELECT * FROM PRPJPLANFEE WHERE PAYBANKID IN (" ;
//  
//  String[] arrPaybankIdEx2 = new String[]{"a","b","c","d","e","f","g"};
//  String strPaybankIdListEx2 = "";
//  
//  for(int i = 0; i < arrPaybankIdEx2.length; i ++ ){
//   
//   if(!"".equals(strPaybankIdListEx2)){
//    strPaybankIdListEx2 += ",";
//   }
//   
//   strPaybankIdListEx2 += arrPaybankIdEx2[i];
//  } 
//  System.out.println("strPaybankIdListEx2 = " + strPaybankIdListEx2);
//  
//  strSQLEx2 += strPaybankIdListEx2;
//  strSQLEx2 += ")";
//  System.out.println("strSQLEx2 = " + strSQLEx2);  
  
  
  
  
  
  
  
  /**
   * example       14
   * ResultSet 
   * */
//   stmt=conn.createStatement(); // dbpool
//       ResultSet rs = null;
//   ResultSet rst = null;
//  
//   rs=stmt.executeQuery("select * from t1");
//
//   rst=stmt.executeQuery("select * from t2");
//
//   rs.last();
  
  
  
  
  /**
   * example       15
   * try catch
   * */
  double res;
  try{
//   res = 1/0;
   testTryCatch();
  }catch(Exception e){
   System.out.println(e.toString() + " use Main");
  }
  
  System.out.println("-----Mainend-----"); // test point
 }
 
 
 
 /**
  * test
  * finally
  * */
 private static boolean f() {
  try {
   return true;
  } finally {
   return false;
  }
 }
  
 /**
  * test
  * try catch throw
  * */
 private static void testTryCatch() throws Exception {
  
  double resTest;
  try {
   resTest = 1/0;
  } catch(Exception e) {
   System.out.println(e.toString() + " use testTryCatch");
//   throw e; // test point
  }
  
  System.out.println("-----testTryCatchend-----"); // test point
 }

}

你可能感兴趣的:(值得思考的文章)