java 面試 —— java 基礎
阿新 • • 發佈:2019-01-10
1. char => int
char 型別轉換為 int 型別時,是轉換為其 ascii 碼或 unicode 碼(比如中文)
char ch = 'A'; int i = ch; // (int)ch // i = 65; int ch = '香'; int i = ch; // (int)ch // i = 39321; \u9999;
2. float
Infinity 與 NaN
public final class Float extends Number implements Comparable<Float> { public static
1/0 java.lang.ArithmeticException thrown: / by zero // 除 0 異常,只有在除數為 0 時才會發生;
3. String
jshell> new String() == new String()
$7 ==> false
jshell> String s = "abc";
s ==> "abc"
jshell> String s2 = new String("abc")
s2 ==> "abc"
jshell> s2 == s
$10 ==> false
jshell> s2.intern() == s
$11 ==> true
java String pool:字串常量池(在堆空間)
String str1 = “ABC”; 可能建立一個物件或者不建立物件。
如果”ABC” 這個字串在java String池中不存在,會在java String池中建立一個String str1= “ABC”的物件。然後把str1指向這個記憶體地址。之後用這種方式建立多少個值為”ABC”的字串物件。始終只有一個記憶體地址被分配,之後都是String的copy。這種被稱為‘字串駐留’,所有的字串都會在編譯之後自動駐留。
原始碼
@Stable private final byte[] value; /** Cache the hash code for the string */ // Java的字串的hash做了快取,第一次才會真正算,以後都是取快取值。 private int hash; // Default to 0