使用两种方式修改String的值:
1.反射
2.Unsafe
代码示例:
import java.lang.reflect.Field;
import org.junit.Test;
import sun.misc.Unsafe;
     public class ModifyString {
     
    
/**
* 反射修改String
* @throws Exception
* 2014年9月23日
*/
@Test
     public void testReflect() throws Exception{
     
    
String str=”hello,world”;
Field f=str.getClass().getDeclaredField(“value”);
f.setAccessible(true);
char[] target=new char[]{‘h’,’a’};
Field c=str.getClass().getDeclaredField(“count”);
c.setAccessible(true);
c.set(str, target.length);
f.set(str, new char[]{‘h’,’a’});
System.out.println(str);
}
/**
* Unsafe 修改String
* @throws Exception
* 2014年9月23日
 
