Are Java wrapper classes really immutable?

后端 未结 6 1703
借酒劲吻你
借酒劲吻你 2020-12-16 12:16

Java Wrapper classes are supposed to be immutable. This means that once an object is being created, e.g.,

Integer i = new Integer(5);

its

6条回答
  •  感动是毒
    2020-12-16 12:50

    The reason i = 6 works is that auto-boxing is intercepting and turning it into i = new Integer(6). Thus as @Peter said, you are now pointing at a new object.

提交回复
热议问题