Using the reflection, the object value change the string array in order to change the string value.

package test;

import java.lang.reflect.Field;

import lombok.Value;

public class Test1{
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String string = "kkk1";
string.hashCode();
String string2 = string;
String string3 = string;
printhashCode(string2);
System.out.println(string2);
System.out.println(string3);


}
static void printhashCode(String string) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Class class1 = string.getClass();
Field declaredField = class1.getDeclaredField("value");
declaredField.setAccessible(true);
char value[] = "kkk".toCharArray() ;
int a = 10;
Object object = declaredField.get(string);
char[] cha = (char[])object;
cha[2] = 'm';
System.out.println(cha);
}
}

I cp to the code here.

In fact, it is the use of reflection, acquiring attribute value string field of the object, and then obtain the corresponding value by the GET string object attribute value.

Then strong into char [], and then change.

Guess you like

Origin www.cnblogs.com/mcmx/p/11332951.html