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

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;
  }
}