1 如下的程式哪個能實現GBK編碼位元組流到UTF-8編碼位元組流的轉換: byte[] src, des; ()
A des=String.fromBytes(src.”GBK”).getByte(“UTF-8”);
B des= new String(src,“GBK”).getByte(“UTF-8”);
C des= new String(“GBK”,src).getByte();
D des=String.encode(String.decode(src,”GBK”),”UTF-8”);
註解:操作的步驟:
* 先解碼在編碼:
* 1 用 new String(src,“GBK”)解碼得到字串
* 2 用 getBytes(“UTF-8”)得到UTF-8 編碼位元組組
2 下述的代碼在發生一次FullGC 嗎,上述代碼在Heap 空間保留的字元數為多少。()
public class Test1 {
public static void main(String[] args) { String string = "0123456789"; String string2 = "0123456789"; String string3 = string2.substring(5);//將前五個字元刪去。 System.out.println(string3); //56789 String string4 = new String(string3); System.out.println(string4.length()); //5 String string5 = new String(string3.toCharArray()); System.out.println(string5.length()); //5}
}
A 5
B 10
C 15
D 20
註解:java的回收機制:
* 記憶體回收主要是針對堆區的記憶體的回收, 因為棧區的回收主要是隨著線程而釋放,堆區分為3個區:
* 年輕代(YoungGeneration), 年長代(OldGeneration), 永久代(PermanentGeneration)
* 年輕代:對象被建立(new)時,本通常放在Young(除了佔據記憶體較大的對象), 經過Minor Gc(針對年輕代記憶體的回收),還活著的對象則會被移動到年老代。
* 年老代:年輕代移動過來的對象稱之為年老代,還有一些佔用記憶體比較大的對象,FullGc主要是針對年老代的記憶體釋放。
* 永久代: 儲存的是final常量,static變數, 常量池
* string4 和string5 是直接New出來的對象, 而substring()其原始碼也是new出來的對象,因為從常量池中截取字串,實則是重新new 一塊記憶體,然後,把截取的字串存進去。