Java代码规范equals, for continue

代码规范equals, for continue

代码规范1
if(v.getPartner().contains("文案")){

}
//修改成:
if("文案".equals(v.getPartner())){

}
//避免因为数据原因导致v.getPartner()为null的情况,然后再调用contains方法导致报空指针异常。

代码规范2
for (Vo vo : ListVo) {
if(判断条件是否如何条件){
//return;
continue;
}
}
//for循环中的使用return提前退出了,而应该使用continue关键字。避免for循环中的数据不会全部执行到。

 

/**
 * 打印输出:
 * id不等于8(字符串与Integer)
 * id等于8(字符串与字符串)
 * id等于8(Integer与Integer)
 */
public class IntegerTest {
    public static void main(String[] args) {
        Integer id = 8;
        if("8".equals(id)){
            System.out.println("id等于8(字符串与Integer)");
        }else {
            System.out.println("id不等于8(字符串与Integer)");
        }

        if("8".equals(String.valueOf(id))){
            System.out.println("id等于8(字符串与字符串)");
        }else {
            System.out.println("id不等于8(字符串与字符串)");
        }


        //如果Integer id = null; 报:Exception in thread "main" java.lang.NullPointerException
        if(8 == id){
            System.out.println("id等于8(Integer与Integer)");
        }else {
            System.out.println("id不等于8(Integer与Integer)");
        }

    }
}

 

posted on 2022-08-05 18:42  oktokeep  阅读(46)  评论(0)    收藏  举报