Java十大低级错误

1、方法和变量命名随意而不规范,没有类注释、方法注释或注释不规范,代码修改后,不同步修改注释,导致注释与代码不符。
2、数据类不重载toString()方法(说明:编程规范要求“所有的数据类必须重载toString() 方法,返回该类有意义的内容”)。
3、对方法的调用不进行空指针判断而造成空指针异常。
4、数据库操作、IO操作的资源没有及时释放,数据库结果集和连接释放顺序不正确,或者使用没有必要的预处理。
5、循环体内包含了大量没有必要在循环中处理的语句,循环体内循环获取数据库连接,循环体内进行不必要的try-catch操作。(说明:编程规范中建议“不要在循环体内调用同步方法和使用 try-catch 块”)
6、嵌套使用try-catch,或者try-catch后面没有必要的finally操作(说明:数据库操作、IO操作等需要使用结束close()的对象必须在try -catch-finally 的finally中close())。
7、不对数组下标作范围校验。
8、equals操作时没有将常量放在equals操作符的左边(说明:字符串变量与常量比较时,先写常量,这样可以避免空指针异常)。
9、字符串转化为数字时没有做异常处理。
10、没有在异常分支记录日志导致问题定位困难。

 

 

1、
 /**
   *
   *@version
   *@see
   * who what when
   * who what when
   *@author
   */
 public class TestFileInp {
   
   /**name*/
   private String name;
  /**testfilename*/
  public static String TESTFILENAME;

   /**
     *
     *@see
     * who what when
     * who what when
     */
   public TestFileInp(){
  
   }

   /**
     *
     *@see
     * who what when
     * who what when
     */
   public static void main(String[] args){
   TestFileInp testFileInp=new TestFileInp();
   System.out.println("testFileInp="+testFileInp);
   }

  /**
     *
     *@see
     * who what when
     * who what when
     */
  public String toString(){
   StringBuffer sb=new StringBuffer(1024);
    sb.append("testFileInp /n")
     .append("{   name=")
     .append(name)
     .append("/n    }");
   return sb.toString();
  }

    /**
     *
     *@see
     * who what when
     * who what when
     */
  public void nullPointTest(String pName,String pArray){
     if(null==pName){
     return;
     }
     if("".equals(pName)){
     }
     System.out.println("pName="+pName);
      //  String[] tArray=new String[]();
     if(null!=pArray){
      for(int i=0;i<pArray.length;i++){
         System.out.println("tArray["+i+"]="+tArray[i]);
      }
     }

  }

  public void dbTest(){
  Class.forName("com...oracle.Driver");
  Connection conn=DriverManager.getConnection(ip,user,passwd);
  Statement st=conn.getStecc();
   String sql="";
  ResultSet rs= st.query(sql);
  while(rs.hasNext()){
   //output
  }
  if(null!=conn){
try{  
 conn.close;
}catch(SQLExc...e ){
  e.
}fially{

  
   if(null!=rs){
     rs.close;
  }
if(null!=st){
     st.close;
  }
  if(null!=conn){
     conn.close;
  }

   File file=new File("");
try{
   InputStream is=new FileInputStream(file);
   byte[] buffer=new byte[4096];

   while(!(-1=is.read(buffer))){
    System....
   }
}catch(){
}fially{
  if(null!=is){
     is.close;
  }
}
 
  
 
  }
 /**
     * <一句话功能简述> <功能详细描述>
     *
     * @see [类、类#方法、类#成员]
     */
    public void controlTest()
    {
        // try catch finally
       
        try
        {
            // while
            while (true)
            {
                System.out.println("test");
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }

 
    /**
     * <一句话功能简述> <功能详细描述>
     *
     * @param pArray
     * @see [类、类#方法、类#成员]
     */
    public void testArray(String[] pArray)
    {
        if (null != pArray && pArray.length > 0)
        {
            for (int i = 0; i < pArray.length; i++)
            {
                System.out.println(pArray[i]);
            }
           
        }
    }
/**
     * <一句话功能简述> <功能详细描述>
     *
     * @see [类、类#方法、类#成员]
     */
    public void testNull()
    {
        String name = null;
        // if (name.equals("wzj"))
       
        if ("wzj".equals(name))
        {
            System.out.println(name);
        }
    }

    public int testNumber()
    {
        String one = "++";
        int tOne = 0;
        try
        {

            tOne = Integer.parseInt(one);
        }
        catch (NumberFormatException e)
        {
            // TODO: handle exception
            return 0;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
        return tOne;
    }
static final String logFile = "error.log";

    public int testNumber()
    {
        URL path = this.getClass().getClassLoader().getResource("/error.log");
        String pathName = path.getPath();
        String one = "++";
        int tOne = 0;
        try
        {

            tOne = Integer.parseInt(one);
        }
        catch (NumberFormatException e)
        {
            // TODO: handle exception
            PrintWriter pw = null;
            try
            {
                pw = new PrintWriter(new File(pathName));
                pw.append("字符转换异常!");
                pw.append("Regular.java line 85");
                pw.flush();

            }
            catch (FileNotFoundException e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            finally
            {
                if (null != pw)
                {
                    pw.close();
                }
            }
            return 0;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
        return tOne;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值