日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
20個(gè)Java優(yōu)秀實(shí)踐,讓你的代碼更絲滑

1 盡可能使用基本類型而不是包裝類型

Long idNumber;
long idNumber; // long 比 Long 占用更少的內(nèi)存

2 為變量選擇合適的類型

如果兩種或多種類型滿足功能需求,請(qǐng)使用占用內(nèi)存空間較小的類型。

int birthYear;
short birthYear; // 更好,因?yàn)槌錾攴莶荒艹^ 32767

int personRunningSpeedKmHour;
byte personRunningSpeedKmHour; // 更好,因?yàn)橐粋€(gè)人的速度不能超過 127 公里/小時(shí)

3 檢查數(shù)字是否奇數(shù),按位與運(yùn)算符比算術(shù)模運(yùn)算符快得多

public boolean isOdd(int num) {
return (num & 1) != 0;
}
// 判斷數(shù)字為奇數(shù)的最佳方法

4 避免冗余初始化

不要用默認(rèn)值來初始化類變量,例如,boolean默認(rèn)情況下值為false,所以用 false 值初始化是多余的。

String name = null; // 冗余
int speed = 0; // 冗余
boolean isOpen = false; // 冗余


String name;
int speed;
boolean isOpen;
// 這樣更清晰

5 盡可能將類成員聲明為私有

public int age; // 非常差
int age; // 差
private int age; // 好

6 在創(chuàng)建字符串時(shí)避免使用'new'關(guān)鍵字

String s1 = new String("AnyString") ; // 不好:初始化慢
// 需要構(gòu)造函數(shù)創(chuàng)建一個(gè)新對(duì)象,并將文字添加到堆中


String s2 = "AnyString" ; // 好:快速實(shí)例化
// 此快捷方式引用字符串池中的項(xiàng)
// 并且僅當(dāng)文字不在字符串池中時(shí)才創(chuàng)建新對(duì)象。

7 對(duì)于多個(gè)字符串連接,使用 StringBuilder 或 StringBuffer

+運(yùn)算符效率低下,因?yàn)?Java 編譯器在創(chuàng)建最終串聯(lián)字符串之前會(huì)創(chuàng)建多個(gè)中間字符串對(duì)象。而StringBuilder或者StringBuffer是在不創(chuàng)建中間 String 對(duì)象的情況下修改 String。

String address = streetNumber +" "+ streetName +" "
+cityName+" "+cityNumber+" "+ countryName; // 差


StringBuilder address = new StringBuilder(streetNumber).append(" ")
.append(streetName).append(" ").append(cityName).append(" ")
.append(cityNumber).append(" ").append(countryName); // 好

注意:StringBuilder不是線程安全的,不是同步的,StringBuffer線程安全和同步的,StringBuilder 比 StringBuffer 更快。

8 在數(shù)字文字中使用下劃線

int myMoneyInBank = 58356823;
int myMoneyInBank = 58_356_823; // 更易讀

long billsToPay = 1000000000L;
long billsToPay = 1_000_000_000L; // 更易讀

9 避免使用索引進(jìn)行“for 循環(huán)”

如果可以用增強(qiáng)的 for 循環(huán)(自 Java 5 起)或 forEach(自 Java 8 起)。因?yàn)樗饕兞咳菀壮鲥e(cuò),因?yàn)槲覀兛赡軙?huì)在循環(huán)體中更改它,或者遍歷的時(shí)候可能會(huì)從 1 而不是 0 開始索引。

for (int i = 0; i < names.length; i++) {
saveInDb(names[i]);
}


for (String name : names) {
saveInDb(name);
} // 更清晰

10 將 try–catch-finally 替換為 try-with-resources

Scanner scanner = null;
try {
scanner = new Scanner(new File("test.txt"));
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
if (scanner != null) {
scanner.close();
}
}
// 容易出錯(cuò),因?yàn)槲覀兛赡軙?huì)忘記在 finally 塊中關(guān)閉掃描器



try (Scanner scanner = new Scanner(new File("test.txt"))) {
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
// 更清晰更簡(jiǎn)潔

11 catch塊不為空

一個(gè)空的catch塊會(huì)使程序默默地失敗并且不會(huì)給出任何關(guān)于出錯(cuò)的信息。

try { 
productPrice = Integer.parseInt(integer);
}
catch (NumberFormatException ex){
}
// 靜默失敗,不提供任何反饋


try {
productPrice = Integer.parseInt(integer);
}
catch (NumberFormatException ex) {
unreadablePrices.add(productPrice); // 處理錯(cuò)誤
log.error("Cannot read price : ", productPrice );// 打印正確且有意義的消息
}

12 盡可能避免空指針異常

通過以下方式盡量避免運(yùn)行時(shí)可能出現(xiàn)的空指針異常:

  • 返回空集合而不是返回 Null 元素
  • 盡可能使用 Optional
  • 使用 java.utils.Objects 的requireNonNull方法
  • 使用NotNull, NotEmpty,NotBlank 注解
  • 在Streams 使用 Objects::nonNull
  • java.util.Objects 中的 requireNonNull方法

13 只添加需要的 getters/setters 和構(gòu)造函數(shù)并避免使用 lombok (YAGNI)

Lombok 是一個(gè)很棒的工具,可以幫助你生成一些樣板代碼,但它有一些缺點(diǎn),例如 IDE 不兼容、使用非公共 API 且與 Java 編譯器緊密耦合。

14 檢查相等性

使用==判斷引用是否相等,使用 equals 判斷對(duì)象是否相等。

// 先決條件:person 類重寫 equals() 和 hashCode() 
Person p1 = new Person ( "John" , "Doe" );
Person p2 = new Person ( "John" , "Doe" );

System.out.println(p1 == p2); // false
System.out.println(p1.equals(p2)); // true

15 在實(shí)現(xiàn) equals 時(shí)始終實(shí)現(xiàn) hashCode

如果你重寫了 hashCode,必須要重寫 equals。
根據(jù) API,如果兩個(gè)對(duì)象的 equals 相等,那么他們的 hashCode() 返回的結(jié)果必須相等。反之則不一定成立。

16 不可變數(shù)據(jù)的 record(自 java14 起關(guān)鍵字)

public final class Person {
private final String name;
private final long idNumber;

public Person(String name, long idNumber) {
this.name = name;
this.idNumber = idNumber;
}

public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Person that = (Person) other;
return (this.name.equals(that.name)) && (this.idNumber == that.idNumber);
}

public String toString() {
return name + " " + idNumber;
}

public int hashCode() {
return Objects.hash(idNumber, name);
}
}



// 這個(gè)類可以轉(zhuǎn)化為一條記錄:
record Person(String name, long idNumber) { } // 更簡(jiǎn)潔

17 定義常量,使用枚舉或 final 類而不是接口

使用 final 類,并定義一個(gè)私有構(gòu)造函數(shù)。

public final class MyValues {
private MyValues() {
// 無(wú)需實(shí)例化類,我們可以隱藏其構(gòu)造函數(shù)
}
public static final String VALUE1 = "foo";
public static final String VALUE2 = "bar";
}

18 注解前加空行

// <-- 空行
@Repository
public class ...

19 靜態(tài)字段應(yīng)該放在類的頂部

20 日期類型處理

建議使用 java.localDateTime(從 Java 8 開始)而不是 java.util.Date 。


分享題目:20個(gè)Java優(yōu)秀實(shí)踐,讓你的代碼更絲滑
本文網(wǎng)址:http://www.5511xx.com/article/dhsjcgo.html