JAVA日常工作容易出錯的地方小匯總

來源:互聯網
上載者:User

以下是同事總結的,感覺挺有用的,貼出來供大家參考

 

 

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
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.