Is the address of an object fixed during its life cycle?

前端 未结 2 527
无人及你
无人及你 2020-12-10 05:25

Is the address of an object constant during its life cycle or can it change? I just thought address of an object never changes. Is it JVM dependent? I Haven\'t found any cle

2条回答
  •  遥遥无期
    2020-12-10 06:17

    Not in general. Many JVM garbage collectors will move objects around, and the language makes no guarantees about the object's location.

    There are some GCs (plain Mark&Sweep, for example) that do not; you can use a specialised JVM that supports these (such as a custom build of Jikes RVM) if you require this feature.

    Note that each object can still contain a (mostly) unique ID (at least in the JVMs that I have seen), partly to support a basic implementation of hashcode.

提交回复
热议问题