set = new HashSet<>(); for (int i = 0 ; i <= Integer.MAX_VALUE; i++){
if (set .contains(i)){
System.out .println("list contains " + i);
}
}
七、使用静态代码块实现赋值静态成员变量
对于集合类型的静态成员变量,应该使用静态代码块赋值,而不是使用集合实现来赋值。
反例:
//赋值静态成员变量反例
private static Map map = new HashMap(){
{
map .put("Leo" ,1 );
map .put("Family-loving" ,2 );
map .put("Cold on the out side passionate on the inside" ,3 );
}
};
private static List list = new ArrayList<>(){
{
list .add("Sagittarius" );
list .add("Charming" );
list .add("Perfectionist" );
}
};
正例:
//赋值静态成员变量正例
private static Map map = new HashMap();
static {
map .put("Leo" ,1 );
map .put("Family-loving" ,2 );
map .put("Cold on the out side passionate on the inside" ,3 );
}
private static List list = new ArrayList<>();
static {
list .add("Sagittarius" );
list .add("Charming" );
list .add("Perfectionist" );
}
八、删除未使用的局部变量、方法参数、私有方法、字段和多余的括号。
九、工具类中屏蔽构造函数
工具类是一堆静态字段和函数的集合,其不应该被实例化;但是,Java 为每个没有明确定义构造函数的类添加了一个隐式公有构造函数,为了避免不必要的实例化,应该显式定义私有构造函数来屏蔽这个隐式公有构造函数。
微信搜索readdot,关注后回复编程资源获取23种精品资料
反例:
public class PasswordUtils {
private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class);
public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES" ;
public static String encryptPassword(String aPassword) throws IOException {
return new PasswordUtils(aPassword).encrypt();
}
正例:
public class PasswordUtils {
private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class);
private PasswordUtils() {}
public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES" ;
public static String encryptPassword(String aPassword) throws IOException {
return new PasswordUtils(aPassword).encrypt();
}
十、删除多余的异常捕获并抛出
用catch 语句捕获异常后,若什么也不进行处理,就只是让异常重新抛出,这跟不捕获异常的效果一样,可以删除这块代码或添加别的处理。
反例:
//多余异常反例
private static String fileReader(String fileName)throws IOException{
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null ) {
builder.append(line);
}
return builder.toString();
} catch (Exception e) {
throw e;
}
}
正例:
//多余异常正例
private static String fileReader(String fileName)throws IOException{
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null ) {
builder.append(line);
}
return builder.toString();
return "fileReader exception";
}*/
}
}
十一、字符串转化使用String.valueOf(value) 代替 " " + value
把其它对象或类型转化为字符串时,使用String.valueOf(value) 比 ""+value 的效率更高。
反例:
//把其它对象或类型转化为字符串反例:
int num = 520 ;
// "" + value
String strLove = "" + num;
正例:
//把其它对象或类型转化为字符串正例:
int num = 520 ;
// String.valueOf() 效率更高
String strLove = String .valueOf(num);
十二、避免使用BigDecimal(double)
BigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常。
反例:
// BigDecimal 反例
BigDecimal bigDecimal = new BigDecimal(0.11 D);
正例:
// BigDecimal 正例
BigDecimal bigDecimal1 = bigDecimal.valueOf(0.11D);
图1. 失去精度
十三、返回空数组和集合而非 null
若程序运行返回null,需要调用方强制检测null,否则就会抛出空指针异常;返回空数组或空集合,有效地避免了调用方因为未检测null 而抛出空指针异常的情况,还可以删除调用方检测null 的语句使代码更简洁。
反例:
//返回null 反例
public static Result[] getResults () {
return null ;
}
public static List getResultList () {
return null ;
}
public static Map getResultMap () {
return null ;
}
正例:
//返回空数组和空集正例
public static Result[] getResults () {
return new Result[0 ];
}
public static List getResultList () {
return Collections.emptyList();
}
public static Map getResultMap () {
return Collections.emptyMap();
}
十四、优先使用常量或确定值调用equals 方法
对象的equals 方法容易抛空指针异常,应使用常量或确定有值的对象来调用equals 方法。
微信搜索readdot,关注后回复编程资源获取23种精品资料
反例:
private static boolean fileReader(String fileName)throws IOException {
return fileName.equals("Charming" );
}
正例:
//调用 equals 方法正例
private static boolean fileReader (String fileName )throws IOException {
return "Charming" .equals (fileName);
return Objects.equals ("Charming" ,fileName);
}
十五、枚举的属性字段必须是私有且不可变
枚举通常被当做常量使用,如果枚举中存在公共属性字段或设置字段方法,那么这些枚举常量的属性很容易被修改;理想情况下,枚举中的属性字段是私有的,并在私有构造函数中赋值,没有对应的Setter 方法,最好加上final 修饰符。
反例:
public enum SwitchStatus {
DISABLED(0 , "禁用" ),
ENABLED(1 , "启用" );
public int value ;
private String description;
private SwitchStatus (int value , String description ) {
this .value = value ;
this .description = description;
}
public String getDescription () {
return description;
}
public void setDescription (String description ) {
this .description = description;
}
}
正例:
public enum SwitchStatus {
DISABLED(0 , "禁用" ),
ENABLED(1 , "启用" );
private final int value ;
private final String description;
private SwitchStatus (int value , String description ) {
this .value = value ;
this .description = description;
}
public int getValue () {
return value ;
}
public String getDescription () {
return description;
}
}
十六、tring.split(String regex)部分关键字需要转译
使用字符串String 的plit 方法时,传入的分隔字符串是正则表达式,则部分关键字(比如 .[]()\| 等)需要转义。
反例:
String [] split = "a.ab.abc" .split ("." );
System .out.println (Arrays .toString (split ));
String [] split1 = "a|ab|abc" .split ("|" );
System .out.println (Arrays .toString (split1));
正例:
// String.split(String regex) 正例
// . 需要转译
String [] split2 = "a.ab.abc" .split ("\\." );
System .out.println (Arrays .toString (split2));
// | 需要转译
String [] split3 = "a|ab|abc" .split ("\\|" );
System .out.println (Arrays .toString (split3));
图2. String.split(String regex) 正反例
逆锋起笔
是一个专注于程序员圈子的技术平台,你可以收获最新技术动态
、最新内测资格
、BAT等大厂的经验
、精品学习资料
、职业路线
、副业思维
,微信搜索逆锋起笔
关注!
你还有什么想要补充的吗?
浏览
19 点赞
评论
收藏
分享
手机扫一扫分享
分享
举报
点赞
评论
收藏
分享
手机扫一扫分享
分享
举报