String hello = "Hello World!";
String xx = new String("Hello World!");
/** *字符串中对于内容和地址的判定可以用下面两种方式,但侧重点不一样。 */ equals // 判断 两个字符串的内容是否一致 == // 判断两个字符串的内存地址是否一致
public static void simple() {
String s1 = "Hello World!";
String s2 = "Hello World!";
String s3 = new String("Hello World!");
String s4 = new String("Hello World!");
// 下面开始比较引用和内容的比较
System.out.println("字符串赋值方式:");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println("\n字符串赋值方式和new方式:");
System.out.println(s1==s3);
System.out.println(s1.equals(s3));
System.out.println("\nnew 方式:");
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
}
字符串赋值方式: true true 字符串赋值方式和new方式: false true new 方式: false true
/**
* Returns a string resulting from replacing all occurrences of
* {@code oldChar} in this string with {@code newChar}.
* <p>
* If the character {@code oldChar} does not occur in the
* character sequence represented by this {@code String} object,
* then a reference to this {@code String} object is returned.
* Otherwise, a {@code String} object is returned that
* represents a character sequence identical to the character sequence
* represented by this {@code String} object, except that every
* occurrence of {@code oldChar} is replaced by an occurrence
* of {@code newChar}.
* <p>
* Examples:
* <blockquote><pre>
* "mesquite in your cellar".replace('e', 'o')
* returns "mosquito in your collar"
* "the war of baronets".replace('r', 'y')
* returns "the way of bayonets"
* "sparring with a purple porpoise".replace('p', 't')
* returns "starring with a turtle tortoise"
* "JonL".replace('q', 'x') returns "JonL" (no change)
* </pre></blockquote>
*
* @param oldChar the old character.
* @param newChar the new character.
* @return a string derived from this string by replacing every
* occurrence of {@code oldChar} with {@code newChar}.
*/
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
new String(buf, true);
private static void deep() throws NoSuchFieldException, IllegalAccessException {
String hello = "Hello World!";
String xx = new String("Hello World!");
String yy = "Hello World!";
/**
* 判断字符串是否相等,默认以内存引用为标准
*/
System.out.println(hello == xx);
System.out.println(hello == yy);
System.out.println(xx == yy);
// 查看hello, xx, yy 三者所指向的value数组的真实位置
Field hello_field = hello.getClass().getDeclaredField("value");
hello_field.setAccessible(true);
char[] hello_value = (char[]) hello_field.get(hello);
System.out.println( hello_field.get(hello));
Field xx_field = xx.getClass().getDeclaredField("value");
xx_field.setAccessible(true);
char[] xx_value = (char[]) xx_field.get(xx);
System.out.println(xx_field.get(xx));
Field yy_field = yy.getClass().getDeclaredField("value");
yy_field.setAccessible(true);
char[] yy_value = (char[]) yy_field.get(yy);
System.out.println(yy_field.get(yy));
/**
* 经过反射获取到这三个字符串对象的最底层的引用数组value,发现如果一开始内容一致的话,java底层会将创建的字符串对象指向同一个字符数组
*
*/
// 通过反射修改字符串引用的value数组
Field field = hello.getClass().getDeclaredField("value");
field.setAccessible(true);
char[] value = (char[]) field.get(hello);
System.out.println(value);
value[5] = '^';
System.out.println(value);
// 验证xx是否被改变
System.out.println(xx);
}
false true false [C@6d06d69c [C@6d06d69c [C@6d06d69c Hello World! Hello^World! Hello^World!
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有