DESKTOP-5NB63LS_20200827-潘振林
问题
1. 字符编码集
我们通常说的默认字符集是指平台的默认字符集,我们可以指定字符编码,这是我们在明面上看到的,而在String的内部对字符所使用的字符编码方案只有两种LATIN1、UTF-16LE
byte[] bytes = { -28, -69, -118, -27, -92, -87, -27, -92, -87, -26,-80, -108 };
String s = new String( bytes ); // 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String
System.out.println( s );
Charset cs = Charset.forName( "UTF-8" );
String r = new String( bytes , cs );
System.out.println( r ); // 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String
2. "String对象" 是不可变的
"abc"和"abcdef"是两个不同的对象
" "中的东西是不会变化的
String s = "abc" ;
System.out.println( s );
System.out.println( System.identityHashCode( s ) );
System.out.println( s.hashCode() );
s = "abcdef" ;
System.out.println( s );
System.out.println( System.identityHashCode( s ) );
System.out.println( s.hashCode() );
String x = "abc" ;
System.out.println( x );
System.out.println( System.identityHashCode( x ) );
System.out.println( x.hashCode() );
//结果发现x指向的"abc"仍是是第一次指向的那个"abc"
心得
String类中零零散散的东西太多了,不好记,还是回去睡觉吧……
点赞