Table of Contents
ToggleJava Convert String to Short
對包含 short 的字串進行數學運算時,通常會使用轉換成 short ,從文本字段或文本區域讀取資料時,輸入的資料都會作為字串接收,將字串轉換為 short ,可以使用 parseShort 、 valueOf 等方法進行操作, Java String Short 任務是將該字串轉換為短整數,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- string
| +- StringToShortTest.java
單元測試
Java String to Short 提供字串轉換成短整數等操作。
parseShort
Java String to Short 使用 Short.parseShort 轉換成短整數,此方法接受另一個參數來更改基數,例如 16(十六進制)或 2(二進制)。
@Test
public void parseShort() {
String value = "155";
short result = Short.parseShort(value);
System.out.println(result);
assertEquals(155, result);
value = "-70";
result = Short.parseShort(value);
System.out.println(result);
assertEquals(-70, result);
value = "70";
// (7)*16^1 + (0)*16^0 = 112
// 96 + 0 = 112
result = Short.parseShort(value, 16);
System.out.println(result);
assertEquals(112, result);
value = "111111";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (1)*2^0 = 63
// 32 + 16 + 8 + 4 + 2 + 1= 63
result = Short.parseShort(value, 2);
System.out.println(result);
assertEquals(63, result);
}
155
-70
112
63
parseShortThrowException
Java String to Short 使用 Short.parseShort 轉換成短整數,輸入不合格式的字串,會拋出例外。
@Test(expected = NumberFormatException.class)
public void parseShortThrowException() {
String value = "155";
short result = Short.parseShort(value);
System.out.println(result);
assertEquals(155, result);
value = "assertj-155";
result = Short.parseShort(value);
System.out.println(result);
}
155
java.lang.NumberFormatException: For input string: "assertj-155"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Short.parseShort(Short.java:118)
at java.lang.Short.parseShort(Short.java:144)
at org.ruoxue.java_147.string.StringToShortTest.parseShortThrowException(StringToShortTest.java:44)
valueOf
Java String to Short 使用 Short.valueOf 轉換成短整數。
@Test
public void valueOf() {
String value = "155";
Short result = Short.valueOf(value);
System.out.println(result);
assertEquals(155, result.shortValue());
value = "-70";
result = Short.valueOf(value);
System.out.println(result);
assertEquals(-70, result.shortValue());
value = "70";
// (7)*16^1 + (0)*16^0 = 112
result = Short.valueOf(value, 16);
System.out.println(result);
assertEquals(112, result.shortValue());
value = "111111";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (1)*2^0 = 63
// 32 + 16 + 8 + 4 + 2 + 1= 63
result = Short.valueOf(value, 2);
System.out.println(result);
assertEquals(63, result.shortValue());
}
155
-70
112
63
valueOfThrowException
Convert String to Short in Java 使用 Short.valueOf 轉換成短整數,輸入不合格式的字串,會拋出例外。
@Test(expected = NumberFormatException.class)
public void valueOfThrowException() {
String value = "155";
Short result = Short.valueOf(value);
System.out.println(result);
assertEquals(155, result.shortValue());
value = "assertj-155";
result = Short.valueOf(value);
System.out.println(result);
}
155
java.lang.NumberFormatException: For input string: "assertj-155"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Short.parseShort(Short.java:118)
at java.lang.Short.valueOf(Short.java:174)
at java.lang.Short.valueOf(Short.java:200)
at org.ruoxue.java_147.string.StringToShortTest.valueOfThrowException(StringToShortTest.java:82)
StringToShortTest.java
Convert String to Short in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.string;
import static org.junit.Assert.*;
import org.junit.Test;
public class StringToShortTest {
@Test
public void parseShort() {
String value = "155";
short result = Short.parseShort(value);
System.out.println(result);
assertEquals(155, result);
value = "-70";
result = Short.parseShort(value);
System.out.println(result);
assertEquals(-70, result);
value = "70";
// (7)*16^1 + (0)*16^0 = 112
// 96 + 0 = 112
result = Short.parseShort(value, 16);
System.out.println(result);
assertEquals(112, result);
value = "111111";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (1)*2^0 = 63
// 32 + 16 + 8 + 4 + 2 + 1= 63
result = Short.parseShort(value, 2);
System.out.println(result);
assertEquals(63, result);
}
@Test(expected = NumberFormatException.class)
public void parseShortThrowException() {
String value = "155";
short result = Short.parseShort(value);
System.out.println(result);
assertEquals(155, result);
value = "assertj-155";
result = Short.parseShort(value);
System.out.println(result);
}
@Test
public void valueOf() {
String value = "155";
Short result = Short.valueOf(value);
System.out.println(result);
assertEquals(155, result.shortValue());
value = "-70";
result = Short.valueOf(value);
System.out.println(result);
assertEquals(-70, result.shortValue());
value = "70";
// (7)*16^1 + (0)*16^0 = 112
result = Short.valueOf(value, 16);
System.out.println(result);
assertEquals(112, result.shortValue());
value = "111111";
// (1)*2^5 + (1)*2^4 + (1)*2^3 + (1)*2^2 + (1)*2^1 + (1)*2^0 = 63
// 32 + 16 + 8 + 4 + 2 + 1= 63
result = Short.valueOf(value, 2);
System.out.println(result);
assertEquals(63, result.shortValue());
}
@Test(expected = NumberFormatException.class)
public void valueOfThrowException() {
String value = "155";
Short result = Short.valueOf(value);
System.out.println(result);
assertEquals(155, result.shortValue());
value = "assertj-155";
result = Short.valueOf(value);
System.out.println(result);
}
}
心得分享
String to Short in Java 轉換方法 parseShort 和 valueOf 似乎完全相同,分別傳回基本型別 short 及物件 Short , valueOf 方法在內部委託給 parseShort 方法,這兩種方法之間存在一個細微差別: valueOf 方法在內部使用整數快取,快取將為 -128 和 127 之間的數字返回相同的 Short 實例,建議使用 valueOf 而不是 parseShort 來取得物件 Short ,因為快取可以為系統提高效率, Convert String to Short in Java 提供將 String 實例轉換為 short 或 Short 實例的多種方法。