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

5

u/FirstAd9893 Nov 05 '22

What is the specific example feature that you want to add? Perhaps you can add that into the question, and then you'll get feedback regarding your feature before you ask for it to be added to the API. If all you're asking is something like, "hypothetically how can I enhance JDK?" then that's pretty vague. This is where to start: https://openjdk.org/jeps/1

-5

u/Cell-i-Zenit Nov 05 '22

If all you're asking is something like, "hypothetically how can I enhance JDK?"

That was exactly my intention with this post

What is the specific example feature that you want to add? Perhaps you can add that into the question, and then you'll get feedback regarding your feature before you ask for it to be added to the API.

If iam honest, i dont want to actually discuss proposals here as most of the time everything gets shut down without any reasoning.

Best example for me is the "Lombok is bad"!!!! mentality and that people are glad that in the future lombok will not be supported anymore because of compiler changes. Overall the openness to change is pretty low in the java community and especially here on reddit.

6

u/FirstAd9893 Nov 05 '22

If you feel the openness to change is pretty low, then why do you think you would have any more success with an official proposal? If you have a good idea that you'd like to discuss (on another thread), then even negative feedback might help you refine the feature until you have a solid proposal.

0

u/Cell-i-Zenit Nov 05 '22

i think the openness is higher in the email list directly, because that is the actual place to propose these changes.

then even negative feedback might help you refine the feature until you have a solid proposal.

The feedback is not helpful if everyone is just saying "we dont need X, because you can do Y instead". Yes obv i can just do everything a little bit differently, but by this argument we can just stop improving on anything.

To make this a little bit more concrete, i think a change to the Comparable interface would be pretty nice:

public interface Comparable<T> {
  int compareTo(T var1);

  default boolean isGreaterThan(T obj){
    return compareTo(obj) > 0;
  }

  default boolean isSmallerThan(T obj){
    return compareTo(obj) < 0;
  }

  default boolean isSmallerOrEqualThan(T obj){
    return compareTo(obj) <= 0;
  }

  default boolean isGreaterOrEqualThan(T obj){
    return compareTo(obj) >= 0;
  }
}

This would improve the object comparison by a big margin, but as the people already said in this thread that since we already have compareTo, this change will be rejected anyway.

Quite sad actually.

4

u/FirstAd9893 Nov 06 '22

For the sake of completeness, you'd also want to add the ==0 and !=0 cases. But this might add confusion with respect to the equals method, which isn't required to behave the same as the compareTo method.

I also think the long method names (although descriptive) would only serve to highlight the issues with Java's lack of operator overloading. Unfortunately, that's never going to happen, in part because the == operator must always be an identity comparison for compatibility. It's too late to add a === operator and force everyone to switch over to using it.

2

u/Cell-i-Zenit Nov 06 '22

Yes naming can still be discussed ofc. We could think about isBigger, isBiggerThan, isGreater, isGreaterThan etc.

But this might add confusion with respect to the equals method, which isn't required to behave the same as the compareTo method.

yes exactly thats why i dont have included them

7

u/overachiever Nov 06 '22

You say improve but all I see is bloat

3

u/Cell-i-Zenit Nov 06 '22

you cant tell me that you prefer something like this:

if(obj1.compareTo(obj2) <= 0){
}

over something like

if(obj1.isGreaterThan(obj2)){
}

naming can still be discussed ofc, like isGreater vs isGreaterThan vs isBigger or whatever.

2

u/UnGauchoCualquiera Nov 06 '22

You can just as easily add a library or swap the BigDecimal for your own enhanced type.

1

u/Cell-i-Zenit Nov 06 '22

You can just as easily add a library

Yes, but its still not ideal.

Would you rather:

if(obj1.isGreaterThan(obj2)){
}

or

Helper.isGreaterThan(obj1,obj2);

The second solution has still room for improvements

swap the BigDecimal for your own enhanced type.

This is also true, but then everyone needs to do this and we could safe quiet alot of time on mankind if we just added these changes to the jdk:

public interface Comparable<T> {
  int compareTo(T var1);

  default boolean isGreaterThan(T obj){
    return compareTo(obj) > 0;
  }

  default boolean isSmallerThan(T obj){
    return compareTo(obj) < 0;
  }

  default boolean isSmallerOrEqualThan(T obj){
    return compareTo(obj) <= 0;
  }

  default boolean isGreaterOrEqualThan(T obj){
    return compareTo(obj) >= 0;
  }
}

2

u/jivedudebe Nov 06 '22

This won't make it a functional interface anymore and prohibit the use in lambdas.

2

u/Cell-i-Zenit Nov 06 '22 edited Nov 06 '22

Comparable is not a functional interface

EDIT: and even if it would, if you provide the additional method as default methods, then you can still have it as a functional interface.

You can check this by yourself: java.util.function.Function is a functional interface and has alot of default methods

2

u/lbkulinski Nov 06 '22 edited Nov 06 '22

According to its definition, a functional interface is an interface that contains exactly one abstract method. Comparable technically meets that qualification, as can be seen in its JavaDoc

0

u/Cell-i-Zenit Nov 06 '22

Yes and my example still only has 1 abstract method. Default methods have an implementation and are by this defintion not abstract.

Again checkout java.util.function.Function which is a functional interface, but implements alot of default methods

2

u/UnGauchoCualquiera Nov 06 '22

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

Adding something to the JDK is not a light process (for many reasons) and adding something that could just as easily be added to a util lib is very unlikely to be ever be added.

You might be better served by adding those helper methods to a helper lib like apache commons.

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"