【问题标题】:CellStyle share the same heap address apache poiCellStyle 共享同一个堆地址 apache poi
【发布时间】:2016-08-26 20:01:42
【问题描述】:

当我在 apache POI 中使用 CellStyle 创建新的单元格样式时,两个样式对象似乎共享相同的堆内存地址。

这怎么可能?

请看下面的例子:

CellStyle style1 =  workbook.createCellStyle();
CellStyle style2 =  workbook.createCellStyle();

当我调试代码时,style1style2org.apache.poi.xssf.usermodel.XSSFCellStyle@a1409f7c 具有相同的内存地址

谁能解释一下这种奇怪的行为?

谢谢 阿斯维尼J

【问题讨论】:

    标签: java apache-poi heap-memory


    【解决方案1】:

    org.apache.poi.xssf.usermodel.XSSFCellStyle@a1409f7c 你观察到的不是内存地址也不是类似的东西,而只是 Object 类的 toString() 方法。

    toString() 的默认实现是classname@hashcode

    很明显,您使用默认构造函数实例化了 2 个对象,因此它们具有相同的状态。因此 hashCode() 的默认实现将为这两个不同的对象提供相同的哈希码。它们具有相同的类和哈希码,因此classname@hashcode 的结果是相同的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      相关资源
      最近更新 更多