Value-based Classes confusion

前端 未结 5 1625
鱼传尺愫
鱼传尺愫 2021-02-05 18:52

I\'m seeking some clarification to the definition of Value-based Classes. I can\'t imagine, how is the last bullet point (6) supposed to work together with the first one

5条回答
  •  死守一世寂寞
    2021-02-05 19:49

    When you execute the lines:

    Optional a = Optional.of(new ArrayList());
    Optional b = Optional.of(new ArrayList());
    assertEquals(a, b); // passes as `equals` delegated to the lists
    

    In the assertEquals(a, b), according to the API :

    1. will check if the params a and b are both Optional
    2. Items both have no value present or,
    3. The present values are "equal to" each other via equals() (in your example this equals is the one from ArrayList).

    So, when you change one of the ArrayList the Optional instance is pointing to, the assert will fail in the third point.

提交回复
热议问题