forked from crossoverJie/JCSprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTest.java
More file actions
39 lines (29 loc) · 973 Bytes
/
StringTest.java
File metadata and controls
39 lines (29 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.crossoverjie.basic;
import java.lang.reflect.Field;
/**
* Function:
*
* @author crossoverJie
* Date: 08/03/2018 13:56
* @since JDK 1.8
*/
public class StringTest {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
String a = "123";
//这里的 a 和 b 都是同一个对象,指向同一个字符串常量池对象。
String b = "123" ;
String c = new String("123") ;
System.out.println("a=b:" + (a == b));
System.out.println("a=c:" + (a == c));
System.out.println("a=" + a);
a = "456";
System.out.println("a=" + a);
//用反射的方式改变字符串的值
Field value = a.getClass().getDeclaredField("value");
//改变 value 的访问属性
value.setAccessible(true) ;
char[] values = (char[]) value.get(a);
values[0] = '9' ;
System.out.println(a);
}
}