博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String和常量池值的变化
阅读量:4221 次
发布时间:2019-05-26

本文共 1734 字,大约阅读时间需要 5 分钟。

 

public static void main(String[] args) {        Integer i1 = new Integer(1);        Integer i2 = new Integer(1);        // i1,i2分别位于堆中不同的内存空间        System.out.println("i1 == i2:" + (i1 == i2));// 输出false        String s1 = "china";        String s2 = "china";        String ss1 = new String("china");        String ss2 = new String("china");        System.out.println("s1 == s2:" + (s1 == s2));        System.out.println("ss1 == ss2" + (ss1 == ss2));        System.out.println("s1 == ss2" + (s1 == ss2));        // 利用Java反射机制改变string的值        Field f = null;        Field f1 = null;        try {            f = s1.getClass().getDeclaredField("value");            f1 = ss1.getClass().getDeclaredField("value");        } catch (NoSuchFieldException e) {            e.printStackTrace();        }        f.setAccessible(true);        f1.setAccessible(true);        try {            f.set(s1, new char[] { 'c', 'h', 'i', 'n', 'b' });            f1.set(ss1, new char[] { 'c', 'h', 'i', 'n', 'b' });        } catch (Exception e) {            e.printStackTrace();        }        System.out.println("改变后的s1的值:" + s1);        System.out.println("s2的值是:" + s2);        System.out.println("ss1的值是:" + ss1);        System.out.println("s1 == s2:" + (s1 == s2));        System.out.println("ss1 == ss2" + (ss1 == ss2));        System.out.println("s1 == ss2" + (s1 == ss2));        System.out.println("============分割线==================");        System.out.println("改变后的ss1的值:" + ss1);        System.out.println("ss2的值是:" + ss2);    }

 

 

打印的结果:

i1 == i2:false

s1 == s2:true
ss1 == ss2false
s1 == ss2false
改变后的s1的值:chinb
s2的值是:chinb
ss1的值是:chinb
s1 == s2:true
ss1 == ss2false
s1 == ss2false
============分割线==================
改变后的ss1的值:chinb
ss2的值是:china

转载地址:http://omemi.baihongyu.com/

你可能感兴趣的文章
js窗体消息提醒
查看>>
深入Hibernate映射文件(二)——<hibernate-mapping>的属性
查看>>
详解在Spring中进行集成测试
查看>>
Hibernate 的工具类
查看>>
Struts2中过滤器和拦截器的区别
查看>>
51单片机:led灯闪烁10次后熄灭
查看>>
安卓:okhttp请求,获取返回数据
查看>>
安卓:股票筛选及分析系统
查看>>
Effective Java 学习笔记一 Object的方法
查看>>
使用 ctypes 进行 Python 和 C 的混合编程
查看>>
用scikit-learn学习DBSCAN聚类
查看>>
机器学习:Python实现聚类算法(三)之总结
查看>>
使用sklearn做单机特征工程
查看>>
Python 多线程技巧 用threading.Event代替time.sleep()
查看>>
工具】Cmake与gcc的关系
查看>>
struct中长度为0的数组用途与原理
查看>>
svm笔记
查看>>
C++ 继承&多态
查看>>
C++多继承的观察和7点体会(都是实用派的观点) good
查看>>
python socket编程详细介绍
查看>>