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 一块内存,然后,把截取的字符串存进去。