Are all primitive wrapper classes immutable objects?

后端 未结 4 2027
逝去的感伤
逝去的感伤 2021-02-07 02:31

Are all primitive wrapper classes in Java immutable objects? String is immutable. What are the other immutable objects?

4条回答
  •  [愿得一人]
    2021-02-07 02:54

    One odd "wrapper" class is Void which doesn't have any valid objects, immutable or otherwise. It can only be set to null.

    One use for Void is to mark generic return types with no value. (You can't use primtive types or void)

    e.g.

    Callable callable = new Callable() {
        public Void call() {
             // do something
            return null;
        }
    };
    

    Even though Date is technically mutable, I would describe it as "immutable by convension". It is generally understood or assumed you wouldn't change a Date object but would replace it to change it like any other immutable object.

提交回复
热议问题