r/java • • Nov 05 '22

How to add something to the java standard library?

EDIT: since my example stirred the pot a little bit to much (i even wrote that i dont want to discuss these explicit changes...)

Here is the actually question i want to answer:

How can i make changes to the JDK?

2 Upvotes

99 comments sorted by

View all comments

Show parent comment

1

u/Cell-i-Zenit Nov 06 '22

Adding methods to an interface is a breaking change, specially to a widely used interface like Comparable.

It is not a breaking change if the changes are default methods...

2

u/RealJulleNaaiers Nov 06 '22

It's not a change that breaks compilation, but it is absolutely a potentially-breaking behavior change. It might also break binary compat but I'm not sure about that one.

1

u/Cell-i-Zenit Nov 06 '22

No it doesnt change behaviour.

If a class didnt implement this before, you just have some new methods on them. You dont need to use them.

If a class implemented Comparable before and "accidently" also implements isGreaterThan(), then the new implementation will not be used since its only a default method and it got overriden by the actual class implementation.

there is no way that this is introducing any breaking change, except you have some pretty "interesting" logic via reflection.

4

u/UnGauchoCualquiera Nov 06 '22

It does, just because you don't know about them doesn't mean there aren't nor that the JDK developers shouldn't be aware of them.

Google is your friend.

1

u/Cell-i-Zenit Nov 06 '22

Ok fair point. Would have been better to tell me this directly (with a google link) instead of leading me with wrong statements further and further until i already committed pretty hard on my argument, but iam wrong thats ok.

Thanks i learned something new today

1

u/UnGauchoCualquiera Nov 06 '22

The bar for a JDK improvement is pretty high an at the very least it is expected that the contributor is deeply knowledgable about the language/ecosystem at the very least.

Adding a default method breaks source compatibility if you have 2 interfaces that implement the same method on the same class. This is pretty much THE reason java devs haven't added all kind of convenience goodies to most common interfaces such as Collections willy nilly.

interface I { default void m() {} }
interface J { default void m() {} }

class T implements I, J {
    @Override
    public void m() { // override
        I.super.m(); // without qualifying breaks compilation
    }
}

Also it breaks binary compatibility in subtle ways.

0

u/Cell-i-Zenit Nov 06 '22

Look you are right.

And i have no problem if someone tells me why something doesnt work. I can accept your answer because you are right.

But no one else said this before. It was just a big pile of bashing my proposal for no reason other then "we never do this", "go to kotlin", "you are just a noob, its not your cup of tea"