CellStyle共享同一个堆地址apache poi
CellStyle share the same heap address apache poi
当我在 apache POI 中使用 CellStyle
创建新的单元样式时,两个样式对象似乎共享相同的堆内存地址。
怎么可能?
请查找以下示例:
CellStyle style1 = workbook.createCellStyle();
CellStyle style2 = workbook.createCellStyle();
当我调试代码时,style1
和style2
都与org.apache.poi.xssf.usermodel.XSSFCellStyle@a1409f7c
具有相同的内存地址
谁能解释一下这种奇怪的行为?
谢谢
阿斯维尼 J
org.apache.poi.xssf.usermodel.XSSFCellStyle@a1409f7c
您观察到的不是内存地址,也不是类似的东西,而只是对象 class.
的 toString() 方法
toString() 的默认实现是 classname@hashcode
。
很明显,您使用默认构造函数实例化了 2 个对象,因此它们具有相同的状态。因此 hashCode() 的默认实现将为这两个不同的对象提供相同的哈希码。它们具有相同的 class 和哈希码,因此 classname@hashcode
的结果相同。
当我在 apache POI 中使用 CellStyle
创建新的单元样式时,两个样式对象似乎共享相同的堆内存地址。
怎么可能?
请查找以下示例:
CellStyle style1 = workbook.createCellStyle();
CellStyle style2 = workbook.createCellStyle();
当我调试代码时,style1
和style2
都与org.apache.poi.xssf.usermodel.XSSFCellStyle@a1409f7c
谁能解释一下这种奇怪的行为?
谢谢 阿斯维尼 J
org.apache.poi.xssf.usermodel.XSSFCellStyle@a1409f7c
您观察到的不是内存地址,也不是类似的东西,而只是对象 class.
toString() 的默认实现是 classname@hashcode
。
很明显,您使用默认构造函数实例化了 2 个对象,因此它们具有相同的状态。因此 hashCode() 的默认实现将为这两个不同的对象提供相同的哈希码。它们具有相同的 class 和哈希码,因此 classname@hashcode
的结果相同。