Copying a HashMap in Java

前端 未结 11 1051
灰色年华
灰色年华 2020-11-29 22:02

I am trying to keep a temporary container of a class that contains member :

HashMap myobjectHashMap

A class called my

11条回答
  •  执笔经年
    2020-11-29 22:29

    In Java, when you write:

    Object objectA = new Object();
    Object objectB = objectA;
    

    objectA and objectB are the same and point to the same reference. Changing one will change the other. So if you change the state of objectA (not its reference) objectB will reflect that change too.

    However, if you write:

    objectA = new Object()
    

    Then objectB is still pointing to the first object you created (original objectA) while objectA is now pointing to a new Object.

提交回复
热议问题