1. 程式人生 > >JavaSE8基礎 String 通過構造方法把部分一維byte數組轉為字符串

JavaSE8基礎 String 通過構造方法把部分一維byte數組轉為字符串

sta 方法 pac pri dem 思考 cas 學習 src



os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)



code:

package jizuiku0;

/*
 * @version V17.09
 */
public class StringBytesDemo {
	public static void main(String[] args) {
		byte[] bs = { ‘a‘, ‘b‘, ‘c‘ };// 存儲的是英文字母的ascii碼
		String str= new String(bs,1,2); // bs數組,從索引值為1開始,提取兩個
										// bs[1] bs[2]
										// 這個方法在 FileInputStream那裏有用
		System.out.println(str);
	}
}


result:
技術分享


sourceCode:

    public String(byte bytes[], int offset, int length) {
        checkBounds(bytes, offset, length);
        this.value = StringCoding.decode(bytes, offset, length);
    }

  這個底層的源代碼很有意思,要花時間研究一下。


Java優秀,值得學習。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。

JavaSE8基礎 String 通過構造方法把部分一維byte數組轉為字符串