default equals again, sorry

Jose jgetino at telefonica.net
Fri Jan 24 10:08:33 PST 2014


Peter, thanks for your detailed explanation.

Yes, I'm kind of confused with anonymous classes. I was thinking that the
passed arguments of the of() factory were retained in a kind of frame along
with the local variables and no room for them would need to be allocated. So
my concern was the frame/heap differences in performance.

But if I've understood you well, the arguments are copied to the heap like
in a regular class, is this correct?

This also explains why I couldn't find any difference in speed performance
with both methods.

   

-----Mensaje original-----
De: Peter Levart [mailto:peter.levart at gmail.com] 
Enviado el: viernes, 24 de enero de 2014 17:39
Para: Jose; 'Zhong Yu'
CC: lambda-dev at openjdk.java.net
Asunto: Re: default equals again, sorry

On 01/24/2014 11:18 AM, Jose wrote:
> Good, it also works.
>
> I tried refactoring the code to get rid of any implantation of the
interface
> and everything works using just the interface - and the abstract class in
> your approach-, so now I have a working interface with no implementations
at
> all.

Hi Jose,

You might be mislead in thinking that in the following code:

interface Point {

     int x();
     int y();

     default int defaultHashCode() { return x() + y(); }

     default boolean defaultEquals(Object o) {
         return o instanceof Point &&
                ((Point) o).x() == x() &&
                ((Point) o).y() == y();
     }

     static Point of(int x, int y) {
         return new Point() {
             public int x() { return x; }
             public int y() { return y; }
             public boolean equals(Object x) { return defaultEquals(x); }
             public int hashCode() { return defaultHashCode(); }
         };
     }
}

...there's no implementation of interface Point and that Points produced 
by static factory method have no "state". The following expression:


         return new Point() {
             public int x() { return x; }
             public int y() { return y; }
             public boolean equals(Object x) { return defaultEquals(x); }
             public int hashCode() { return defaultHashCode(); }
         };


Produces an instance of an anonymous inner class, which implements the 
Point interface. This is an almost ordinary class with name generated by 
javac compiler, so you can't reference this class by name in code, but 
that doesn't mean it is "stateless".

Sometimes JVM JIT can make the "state" disappear. For example, if you do 
this in a loop:

     sum += Point.of(x, y).x();

... it will effectively be translated to:

     sum += x;

But so will the following:

     sum += new PointImpl(x, y).x();


The style of programming (using anonymous vs. named Java classes) does 
not play any difference here.


Regards, Peter

> Regarding execution speed I couldn't say any difference between the
> statefull/stateless versions, but this might say nothing if the burden of
> performing long math computations is much greater that the one of
> instantiating a (x,y) or accessing 2 local variables. And regarding memory
> overhead I feel it would be difficult to test.
>
>
>   
>
> -----Mensaje original-----
> De: Zhong Yu [mailto:zhong.j.yu at gmail.com]
> Enviado el: viernes, 24 de enero de 2014 6:55
> Para: Jose
> CC: John Rose; lambda-dev at openjdk.java.net
> Asunto: Re: default equals again, sorry
>
> Any objection to an intermediary abstract class with default equals()?
>
>      abstract class AbsPoint{
>          public boolean equals(Object)...
>
>      ...
>      return new AbsPoint(){
>          public int x()...
>          public int y()...
>
>
>
> On Thu, Jan 23, 2014 at 10:20 PM, Jose <jgetino at telefonica.net> wrote:
>> I see thanks; I have to replicate the code in each factory.
>>
>> On the other way, do you think that this idiom would lead to a fewer
>> overhead than instantiating (x,y) points?.
>>
>> The scenario is a chain of 2D transforms (over an image for example),
> every
>> step would require a new pair to be created.
>>
>> Paul, I want to compare this with a stock implementation, but I don't
need
>> it, I only want a factory to create anonymous classes from the interface.
>>
>>
>>
>> -----Mensaje original-----
>> De: John Rose [mailto:john.r.rose at oracle.com]
>> Enviado el: jueves, 23 de enero de 2014 23:04
>> Para: Jose
>> CC: lambda-dev at openjdk.java.net
>> Asunto: Re: default equals again, sorry
>>
>> On Jan 23, 2014, at 1:02 PM, "Jose" <jgetino at telefonica.net> wrote:
>>
>>
>>          interface Point(){
>>          int x();
>>          int y();
>>          static Point of(int x, int y) {
>>            return new Point() {
>>                public int x() {
>>                      return x;
>>                }
>>                public int y() {
>>                    return y;
>>                }
>>
>>
>> +      public boolean equals(Object x) { return defaultEquals(x); }
>> +      public int hashCode() { return defaultHashCode(); }
>>
>>
>>             };
>>          }
>>
>>
>>
>>
>>
>



More information about the lambda-dev mailing list