一、Labmda表達式:
Runnable runnable =new Runnable(){
public void run(){
//123
}
}
變:
Runnable runnable =()->{
//123
}
“123”爲多句代碼,可以寫上括號,如果僅一句,可以不寫。
二、獲得小數位數:
經測試,使用小數(1.01)通過使用“.”進行split操作後,得到的數組爲空,原因在於String的split方法
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
...
}
return Pattern.compile(regex).split(this, limit);
}
if語句判斷無法通過,因爲不允許使用"."做split的regex字符;第2個條件明顯長度只有1;第3個條件ch<55276或者ch>57344才滿足。
運行到return語句,Pattern執行結果,獲得空數據。
那麼只能通過indexOf方法來獲得小數位數了:
1位小數且小數第1位是0的(如1.0),顯示整數;其他情況顯示原小數(String默認會把1.10看成1.1)。
public static String get1BitData(double number) {
String first = String.valueOf(number);
String second = first.substring(first.indexOf("."));
if (second.length() > 1 && !second.endsWith("0")) {
return String.valueOf(number);
}
return String.valueOf((int) number);
}
三、取相應格式的小數:
- // 取一位整數
- System.out.println(new DecimalFormat("0").format(pi));// 3
- // 取一位整數和兩位小數
- System.out.println(new DecimalFormat("0.00").format(pi));// 3.14
- // 取兩位整數和三位小數,整數不足部分以0填補。
- System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
- // 取所有整數部分
- System.out.println(new DecimalFormat("#").format(pi));// 3
- // 以百分比方式計數,並取兩位小數
- System.out.println(new DecimalFormat("#.##%").format(pi));// 314.16%
- long c = 299792458;
- // 顯示爲科學計數法,並取五位小數
- System.out.println(new DecimalFormat("#.#####E0").format(c));// 2.99792E8
- // 顯示爲兩位整數的科學計數法,並取四位小數
- System.out.println(new DecimalFormat("00.####E0").format(c));// 29.9792E7
- // 每三位以逗號進行分隔。
- System.out.println(new DecimalFormat(",###").format(c));// 299,792,458
- // 將格式嵌入文本
- System.out.println(new DecimalFormat("光速大小爲每秒,###米。").format(c));
四、String文件中的代替符
-:–
對整數進行格式化:%[index$][標識][最小寬度]轉換方式
我們可以看到,格式化字符串由4部分組成,其中%[index$]的含義我們上面已經講過,[最小寬度]的含義也很好理解,就是最終該整數轉化的字符串最少包含多少位數字。我們來看看剩下2個部分的含義吧:
標識:
'-' 在最小寬度內左對齊,不可以與“用0填充”同時使用
'#' 只適用於8進制和16進制,8進制時在結果前面增加一個0,16進制時在結果前面增加0x
'+' 結果總是包括一個符號(一般情況下只適用於10進制,若對象爲BigInteger纔可以用於8進制和16進制)
' ' 正值前加空格,負值前加負號(一般情況下只適用於10進制,若對象爲BigInteger纔可以用於8進制和16進制)
'0' 結果將用零來填充
',' 只適用於10進制,每3位數字之間用“,”分隔
'(' 若參數是負數,則結果中不添加負號而是用圓括號把數字括起來(同‘+’具有同樣的限制)
轉換方式:
d-十進制 o-八進制 x或X-十六進制
System.out.println(String.format("%1$,09d", -3123));
System.out.println(String.format("%1$9d", -31));
System.out.println(String.format("%1$-9d", -31));
System.out.println(String.format("%1$(9d", -31));
System.out.println(String.format("%1$#9x", 5689));
System.out.println(String.format("%02x", 5689));// 取兩位數的16進制
結果:
32
-0003,123
-31
-31
(31)
0x1639
1639
其他:
Ioc反轉和Di注入的可用容器有Spring、JBoss、Ejb等。
它們都是一種編程思想,目的是解耦。
A調用B,正常情況下是,創建B,再調用B,則A依賴B,並且使用完畢還要銷燬B。
Ioc,A告訴容器要調用B,容器創建B,並通知A,然後A通過構造函數、屬性和接口調用方式,獲得B,再去調用B。
Di,A告訴容器要調用B,容器創建B,並通知A,然後A通過反射的方式,獲得B,再去調用B。
AOP:Aspect Oriented Programming。面向切面編譯,切面指某類的某方法的代碼片斷,切點(JoinPoint)指某類的某方法。AOP指通過動態注入的方式,將幾處共用的代碼片斷,在使用時注入,使得代碼維護更加便利。
【Java1024問】:一網打盡Java熱門問題,阿里百位技術專家答疑解惑 https://yq.aliyun.com/ask/119478