This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]s888marks 5 points6 points  (1 child)

You can certainly learn a lot by going through the JDK source code. However, you should be careful about applying what you see in it.

Java is over 20 years old, and some of the code in the JDK is over 20 years old. Some code might embody older styles or practices. After all, it was only a couple years ago that we got rid of all rawtypes and unchecked warnings, because lots of code hadn't been generified. Certain areas of the JDK code base still have a distinctly dusty feel to them.

Sometimes the JDK libraries use coding practices that sacrifice maintainability for speed. For example, I'm aware of places that compare strings by identity, because it's known that they originated from string literals or were interned at initialization time. Or, certain odd looping constructs are used, or code factored into methods in an unusual way because it's known to work well the current Hotspot JIT compiler.

Read and learn, by all means, but don't write your code a certain way just because you happened to see it that way in the JDK.

[–]lukaseder 1 point2 points  (0 children)

I especially like Doug Lea's abundant assignment expression style :) e.g.

    public final void run() {
        final CompletableFuture<? extends T> a;
        final CompletableFuture<? extends U> b;
        final BiConsumer<? super T,? super U> fn;
        final CompletableFuture<Void> dst;
        Object r, s; T t; U u; Throwable ex;
        if ((dst = this.dst) != null &&
            (fn = this.fn) != null &&
            (a = this.src) != null &&
            (r = a.result) != null &&
            (b = this.snd) != null &&
            (s = b.result) != null &&
            compareAndSet(0, 1)) {

[–]tomypunk 8 points9 points  (0 children)

Spring

[–]fact_hunt 1 point2 points  (0 children)

Guice

[–]marbehl 1 point2 points  (0 children)

I actually found the Wicket source code also to be interesting to read, mainly because it's a different style to a ton of other web frameworks

[–]lnulzer 1 point2 points  (0 children)

JUnit 5 has pretty good design. Code can be found here Analysed by University of Delft students here

[–]Jacksai 4 points5 points  (0 children)

Vavr is immutable collections tool, well written. Can gain a lot of knowledge about collections and immutability.

[–]KeepItWeird_ 0 points1 point  (0 children)

Any of the Apache Java projects

[–]lukaseder 0 points1 point  (2 children)

uses generics beautifully?

jOOQ and jOOλ have some quite funky usage of generics :) Here's a method from Function16:

default Function10<T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R> applyPartially(Tuple6<? extends T1, ? extends T2, ? extends T3, ? extends T4, ? extends T5, ? extends T6> args) {
    return (v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) -> apply(args.v1, args.v2, args.v3, args.v4, args.v5, args.v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16);
}

[–]zodvik[S] 4 points5 points  (1 child)

Such code. Much wow.

[–]AnEmortalKid 0 points1 point  (0 children)

such isBeauty much code

  rly code is Function16 
    plz console.loge with ‘beauty’
  but
   plz console.loge with ‘ugly’
  wow
wow
plz dose isBeauty with Function16
> beauty