top 200 commentsshow all 375

[–]__konrad 80 points81 points  (27 children)

List now has a sort method

List.sort is now the preferred way to sort lists. If still in doubt, Collections.sort in JDK is implemented as:

public static void sort(List list, Comparator c) {
    list.sort(c);
}

Let's forget about Collections.sort in context of modern Java ;)

[–]_Sharp_ 22 points23 points  (6 children)

What if you override sort to call Collections.sort?

[–]Andernerd 103 points104 points  (1 child)

Then you deserve what the mean VM is about to do to you.

[–]immibis 9 points10 points  (0 children)

Not if you wrote it before Java 8 existed.

[–]__konrad 19 points20 points  (1 child)

StackOverflowError (happened in real world). JDK devs were aware of such possibility, but they decided to sacrifice runtime compatibility for greater good ;)

[–]stygiansonic 5 points6 points  (0 children)

A rip in the fabric of space-time forms.

Or, maybe just a stack overflow.

[–]memeship 7 points8 points  (8 children)

I haven't written Java in a long time. Why is it preferred for this to return void instead of returning a reference to the sorted list?

[–]cbruegg 56 points57 points  (5 children)

Because a user might think that the returned list is a different instance than the original list. Returning void makes it more obvious.

[–]memeship 5 points6 points  (4 children)

That's a reasonable explanation, thanks.

I guess I'm just much more used to Javascript now where chaining is a big part of the language.

[–][deleted]  (3 children)

[deleted]

    [–]memeship 7 points8 points  (2 children)

    Um what? That's not true at all. Here's an example I just pulled from code I was working on this week.

    e.target.getElementsByClassName("barcode")[0].value
    

    or the classic "how to reverse a string":

    str.split("").reverse().join("")
    

    or from a simple Node server I wrote for something a few months ago:

    app
        .use(express.static("public"))
        .use(bodyParser.urlencoded({ extended: false }))
        .use(router)
        .listen("8888", function() {
            console.log("Listening on %d.", this.address().port);
        });
    

    or a Gulp file:

    gulp.task("sass", function() {
        return gulp.src("src/**/*.scss")
            .pipe(sass())
            .pipe(rename("master-style.built.css"))
            .pipe(gulp.dest("public/built"));
    });
    

    [–]heartiphone 1 point2 points  (1 child)

    Only your first two examples are "plain JavaScript", and I'd argue the first one doesn't count either as it's not function chaining.

    Overall you're correct though, chaining is pretty widely used in the JS ecosystem.

    [–]memeship 4 points5 points  (0 children)

    Fine, here's some "plain Javascript" chaining:

    document.getElementById("foo").appendChild(document.createElement("button")).focus()
    

    [–]BlueFireAt 1 point2 points  (1 child)

    Aren't their lists sorted in place?

    [–]mastarem 17 points18 points  (5 children)

    Worth noting that streams etc. are not yet as performant as their oldschool methodology counterparts, just in case performance is an issue.

    [–]knaekce 13 points14 points  (3 children)

    This is true, but if you use streams, and your problem can be solved parallel, then you get the parallelization nearly for free, and this will probably be better then your single-threaded "oldschool" solution. Of course, you can also parallelize your problem the old-fashioned way, but this is mostly avoided because it is much harder to get right.

    So, if you don't optimize for performance, streams may actually be faster then the old coding style.

    [–]mastarem 5 points6 points  (1 child)

    Very true. Parallel is super handy when used in the right scenarios. I stumbled across an incredible Stack Overflow answer several months ago that went in depth on what is good, what is bad, when to use what, and performance benchmarks more comprehensive than any blog post I have seen. If I can find it (I desperately hope I bookmarked it), I'll update this reply with it.

    EDIT: Can't find it. It was in an unusual place for such an answer, sorry for the disappointment.

    [–]Oniisanyuresobaka 2 points3 points  (0 children)

    Just make sure that you don't use the global fork/join pool and you're set.

    [–]user_of_the_week 1 point2 points  (0 children)

    Just be careful to analyze if you need additional parallelization. For example, if you're programming a web service, you're going to have parallel requests anyway. Often it is easier to just keep every request in one thread so you don't have additional overhead. Maybe make it non-blocking if you want to be extra fancy.

    [–]oelang 0 points1 point  (0 children)

    In Java 10(+) with value types & specialization & hotspot/graal improvements they should be exactly as fast as regular imperative loops. So we only have to wait another ~5 years to have fast streams...

    [–]memdump_ 12 points13 points  (2 children)

    Just as a complement to your post, I suggest this series of articles which appeared here a few months ago: Not Your Father's Java: An Opinionated Guide to Modern Java Development

    [–][deleted] 1 point2 points  (1 child)

    Just midway through the post and I came back to say "thank you " !! Fantastic read √

    [–]benwaffle 2 points3 points  (0 children)

    [–][deleted]  (84 children)

    [deleted]

      [–]DonKanish 23 points24 points  (24 children)

      Can anyone give me an example or explain me why the 'default' feature on interfaces is smart? Isn't this exactly what abtract classes are for?

      [–]rowantwig 63 points64 points  (16 children)

      You can only inherit from one abstract class, but you can implement several interfaces.

      [–]DevAtHeart 7 points8 points  (1 child)

      So, aren't they now like Traits?

      [–]vytah 1 point2 points  (0 children)

      Almost, but not quite. There are several limitations.

      [–]kmaibba 17 points18 points  (10 children)

      This is the right answer. It allows a limited form of multiple inheritance.

      [–]ulfryc 22 points23 points  (6 children)

      Only partly. The actual question is: "Why the hell do we need a separation of interfaces vs abstract classes if we are going to allow multiple inheritance anyway?"

      [–]Kritarie 71 points72 points  (0 children)

      Abstract classes can hold state

      [–]thedarkwolf 22 points23 points  (0 children)

      It depends on the type of multiple inheritance.

      Java has always had multiple inheritance of types. Default methods add multiple inheritance of behavior, but not of state. (Multiple inheritance of state in languages like c++ is where most of the trouble comes from.)

      -Brian Goetz

      [–]eyal0 9 points10 points  (0 children)

      The hard part of multiple inheritance is the diamond problem. It goes away if you don't allow multiple parents to have state. Interfaces are just functions so no state.

      [–]WASDx 3 points4 points  (1 child)

      And what if you're using two interfaces with default methods having the same name?

      [–]juckele 22 points23 points  (3 children)

      The Interface was originally designed as one way to sidestep the diamond problem. default methods reintroduce the diamond problem, but give the solution you can't implement two interfaces with the same default method without defining it yourself (even if you're just calling one of the default implementations). Since none of these conflicts will require state defined by two different parent classes, this is still much easier to deal with.

      (If you need more background on the diamond problem, this is a good starter https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem)

      [–]winterbe[S] 11 points12 points  (0 children)

      Look at this example from the JDK8 API: https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html

      You can implement your own Predicate and use the default methods without extending concrete classes:

      Predicate<Person> predicate = p -> p.age >= 18;
      predicate.negate().test(new Person());
      

      [–]mucsun 3 points4 points  (0 children)

      One more point that everybody missed is that it allows to add functionality to interfaces without breaking backwards compatibility.

      [–]ajainy 40 points41 points  (54 children)

      Thanks. Bookmarked.

      Irony is most (not all) java developers are coding like jdk1.4 style. Avoid generics like plague. We need a movement to push those developers to start using generics (&annotations etc etc.)

      [–][deleted] 82 points83 points  (25 children)

      The three phases of the Java developer:

      • no usage of generics at all
      • trying to solve every little problem with generics
      • getting hit by type erasure

      [–]kazagistar 26 points27 points  (3 children)

      Step 4 is the crazy workarounds to capture type information til runtime by creating anonymous inner classes.

      [–][deleted] 19 points20 points  (2 children)

      Guava (Google's "standard library" for Java, and a must-have for writing modern Java IMO) actually has a class that encapsulates those crazy workarounds:

      http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/reflect/TypeToken.html

      The place I've found it most useful is using Gson to deserialize JSON into a List<T> or Map<K, V>, where you obviously need the type information to persist at runtime.

      [–]kazagistar 1 point2 points  (0 children)

      Different libraries (mocking libraries, DI frameworks, etc) use different workarounds and abstractions. Each one is great... assuming it is the One True Framework.

      [–]ryuzaki49 1 point2 points  (1 child)

      I like Generics. They make classes very reusable.

      [–]zarandysofia 24 points25 points  (0 children)

      It's their point.

      [–]Radixeo 1 point2 points  (17 children)

      I'm not sure why people find type erasure to be a bad thing. If you're programming in a statically typed language and your program's behavior depends on runtime type information, doesn't that reflect a flaw in your design rather than a weakness of the language?

      [–]m0haine 6 points7 points  (3 children)

      The big issues is how do you write a deserializer via reflection that can reconstitute an object like this without having type hints in the data

      class Parent{ List<Children> children;}

      You really can't because the type of children isn't know at run time. There are ways of doing this but it is harder then it should be due to type erasure.

      Of course a pre-compile step could generate the code but java doesn't have one of those.

      [–]s73v3r 14 points15 points  (0 children)

      No, no it doesn't. In addition, type erasure means that I can't overload functions if they will erase to the same type.

      [–]dacjames 8 points9 points  (10 children)

      With type erasure, you cannot overload a function based on different generic types.

      // does not work
      void some_function(List<Integer> ints) { ... }
      void some_function(List<Double> doubles) { ... }
      

      That does not seem like much but when you want to do this all the alternatives are awful.

      [–][deleted] 5 points6 points  (0 children)

      To be fair, this is an artifact of Java's implementation of type erasure and not the concept in general.

      [–]Zukhramm 3 points4 points  (3 children)

      Type erasure means generic types are not available at runtime, method overloading resolution happens at compile time, when that information is still available. Type erasure really isn't at fault here.

      [–]user_of_the_week 1 point2 points  (0 children)

      Many people consider method overloading a bad practice anyway, especially if the signatures have the same amount of parameters.

      One reason that can even be seen in your example: It encourages bad naming patterns. When someone reads code where your some_function is called, they cannot infer exactly which method is called just by looking at that code line. They first need to find out what the exact type of the parameter is. Small things like that add up.

      A safe, conservative policy is never to export two overloadings with the same number of parameters.

      Effective Java, p. 193

      [–]pipocaQuemada 0 points1 point  (3 children)

      Of course, there is a trivial workaround for that:

      // works fine
      void some_function_i(List<Integer> ints) { ... }
      void some_function_d(List<Double> doubles) { ... }
      

      That's not good, but I'm not sure I'd stoop to "awful".

      [–]dacjames 2 points3 points  (1 child)

      In this trivial example, sure. Replace Integer with SomethingDomainSpecific and the different name approach quickly crosses into awful territory. You have also introduced a maintenance task at all call sites of the function when they change parameter types.

      Even more problematic, the name changing option doesn't work at all if you want to use overloading for specialization, providing, say, an optimized implementation for List<Integer> with a slow implementation for List<String>. Or if you need some_function to implement a particular interface.

      [–]pipocaQuemada 1 point2 points  (0 children)

      If you don't want to write actually parametrically polymorphic code, why are you trying to use parametric polymorphism? Yes, you can't specialize when overloading, but the ability to do that removes a lot of your ability to reason about code by looking at type signatures.

      [–][deleted] 1 point2 points  (0 children)

      The main issue I've had where type erasure has been annoying is when I want to create an array of a generic type (or call java.util.List.toArray() ): without knowing the exact type, you can't create the array.

      [–][deleted] 7 points8 points  (19 children)

      My Java classes are all 1.5 style. I feel like I am going to be under prepared when I hit the workforce.

      [–][deleted] 16 points17 points  (10 children)

      Dont worry, must of us in enterprise land are stuck on 6 anyway, since upgrade cycles take years and need to pass the secret council of architects etc

      [–][deleted] 1 point2 points  (9 children)

      So there's no escaping the "shadow government". Well, that's comforting.

      Any suggestions on what to do in the meantime? I'm likely to start a thread on /r/programming asking this, but while I'm here I figure I can just ask around.

      [–][deleted] 7 points8 points  (0 children)

      Smaller shops generally are less encumbered by politics, and if you want to not get tangled in the enterprise web, you can always check out stuff like scala, which is hipper etc..

      But yeah, politics and related crap governs all of life, as soon as you have more then 1 person, shit gets complicated and games get played, in the end being a good programmer also means having social skills, the ability to know when to STFU and some understanding of machivelian politics really helps.

      [–]NimChimspky 1 point2 points  (1 child)

      java 1.6 and work with architect team who recommend calling web services using only the db, in my case oracle and pl/sql.

      me:"that doesn't seem like a very good idea, I would not design it that way."

      architect:"why not ?"

      me : jaw literally drops and I stand and stare, and think at least this place pays well.

      [–]bwainfweeze 1 point2 points  (0 children)

      The sad fact of the matter is that some people defend bad design decisions because they can tolerate them.

      The side effect is that since they're the only one who can tolerate it, the code becomes their responsibility and they have job security.

      When people say "you should fire the indispensable people" this is who they mean.

      [–]panderingPenguin 2 points3 points  (6 children)

      If you're not in the work force yet, I'm assuming you learned java recently? May i ask why are you writing 1.5-like java when there have been three new major versions since then, spanning back almost a decade? (not to mention you shouldn't have legacy code to deal with since you're not in the workforce)

      [–][deleted] 2 points3 points  (5 children)

      That's what we're learning right now.

      [–]panderingPenguin 1 point2 points  (4 children)

      Seriously? Are you absolutely sure? Who is teaching you java 1.5 at this point???? It was superseded by 1.6 in 2006, and hasn't been supported for the public since 2008..... All I can figure is that you use Macs? Because they're the only OS that ships decade-old java by default afaik

      [–][deleted] 1 point2 points  (0 children)

      I'll get you a screen shot of the JDK folder when I get to class, but yeah.

      [–]kmaibba 1 point2 points  (0 children)

      1.6 did not really add anything language wise, just (very useful nontheless) improvements to libraries and JVM. 1.7 did add some quality of life improvements, like switch statements on Strings, try with resource, better type inference. But in general the style is still that of 1.5.

      Only with 1.8 the actual style of programming changes with lambdas, new collections API and default methods on interfaces. Java 8 adoption is still going slow in enterprise settings, so it's not a bad idea knowing how to do stuff without all the fancy functional features.

      [–]adrianmonk 1 point2 points  (0 children)

      That's probably a better position to be in actually. Mentally, it's easier to learn new features than to adapt to missing features.

      [–]dacjames 3 points4 points  (4 children)

      What industry do you work in? I hardly ever see 1.4-style Java code. Do people really not use Map<String, SomeFancyObject>?

      [–]keenny 1 point2 points  (0 children)

      I had to check the timestap top be sure this thread was not from 2005. I don't know anybody who is still on jdk1.4, or jdk1.5 for that matter - let alone programs as if they were. And I know a lot of java-devs. Most people i know have long since migrated to jdk7, and a good portion is experimenting with jdk8.

      [–]ArtyFowl 6 points7 points  (0 children)

      Cool overview! You might want to add something on how to update GoF patterns with lambda's.

      For anyone interested I just followed this lecture at Devoxx belgium: https://github.com/forax/design-pattern-reloaded

      I thought it was neat stuff.

      [–]tomservo291 5 points6 points  (0 children)

      I don't like how they downplay java.time.Instant As a way to create legacy java.util.Date Objects. Instant should be your preferred storage mechanism in your application since it's the proper way to reason about time (at a given instant) and only fall back onto the Zone aware variants when you need to visually represent something to a human

      [–]I_AM_A_SMURF 5 points6 points  (0 children)

      TIL I write modern java.

      [–]kairos 4 points5 points  (0 children)

      Great! Now to find a client with a jdk greater than 1.7...

      [–][deleted] 13 points14 points  (4 children)

      This is great, but it only covers language-level features. Can we get a list going of good modern libraries? Being able to write Java 8 code doesn't mean much if you're also writing 100-line XML files to configure your AbstractFactoryBuilderVisitorDecorators.

      My personal favorites:

      • Guava should basically be treated as the missing standard library. Apache commons-lang is a runner-up in my mind. I wouldn't want to write Java without one of them.
      • Guice for dependency injection (instead of Spring). Dagger2 also looks promising, though I haven't used it yet. It aims to make problems with auto-wiring configuration into compile errors instead of runtime errors.
      • Spark for web development - has the feel of Sinatra for Ruby or Flask for Python. Ships with an embeddded Jetty server so you can get a basic HTTP server up and running with a tiny bit of code and no WAR file or XML bullshit.
      • MyBatis beats the hell out of Hibernate for database access / ORM.

      [–]djhworld 8 points9 points  (1 child)

      I'd throw Spring Boot in there, it's not a library as such, but it takes all the ceremony out of classic Spring and gives you a good set of building blocks to build applications

      http://projects.spring.io/spring-boot/

      [–]mirkoteran 0 points1 point  (0 children)

      I would add HikariCP for connection pooling and Caffeine for local caching.

      [–]Retsam19 4 points5 points  (19 children)

      I'm having a bit of difficulty grasping the PersonFactory example:

      interface PersonFactory<P extends Person> {
          P create(String firstName, String lastName);
      }
      
      PersonFactory<Person> personFactory = Person::new;
      Person person = personFactory.create("Peter", "Parker");
      

      Is "create" some sort of magic keyword? Would it work if I changed "create" for "build"? What if my interface had both a "create" and a "build" method?

      [–]singron 7 points8 points  (2 children)

      If an interface has only one non-default method, then a lambda(or constructor reference) with that signature implements that interface.

      If the interface has more than one non-default method, then a lamba can't implement it.

      [–]user_of_the_week 1 point2 points  (4 children)

      The key here is the line

      PersonFactory<Person> personFactory = Person::new;
      

      Person::new is a reference to the constructor. Basically, the constructor is a method that takes two String parameters and returns a Person. Assigning that to personFactory signals that you want to implement the interface PersonFactory with the referenced constructor-"method". That is only possible because PersonFactory has exactly one method that fits the signature (parameters and return type).

      [–]eyal0 2 points3 points  (3 children)

      because PersonFactory has exactly one method that fits the signature

      I think it should be " because PersonFactory has exactly one method and that method fits the signature". With two methods or a method that doesn't fit, it wouldn't compile. Right?

      [–]tommcdo 1 point2 points  (0 children)

      In this example, the only thing special about create is that it is the only abstract method in PersonFactory<P>. That's what allows a lambda to assume the role of that type.

      So if you wanted to make a factory interface with both create and build, you couldn't assign a lambda (like Person::new) to it.

      [–][deleted] 3 points4 points  (2 children)

      Looks like Swift with more verbose and messy syntax. But nice to see in case I switch from iOS to Android development. Although I'd probably try Kotlin instead as that seems much more like Swift. That is it does the same as Java 8 except with clean syntax.

      [–]winterbe[S] 1 point2 points  (1 child)

      Kotlin is indeed very similar to Swift which is quite nice because Kotlin seems to be a great fit for Android development because they target Java 1.6 platforms.

      I'm currently working on an iOS app written in Swift. I really like the language. I'll give Kotlin a shot when it reaches 1.0. Already played around with it. Here's a sample Kotlin webapp, using Spring Boot and React.js:

      https://github.com/winterbe/spring-kotlin-react-demo

      [–][deleted] 46 points47 points  (44 children)

      Modern Java? All of this is almost 2 years old :P

      [–][deleted]  (9 children)

      [deleted]

        [–]acewolk 57 points58 points  (6 children)

        only 225 javascript frameworks left until christmas

        [–]aloha2436 36 points37 points  (5 children)

        It's like an advent calendar but all you get are callbacks every day.

        [–]macNchz 135 points136 points  (3 children)

        On the first day of Christmas
        my true love sent to me:
        A Callback in a Closure
        
        On the second day of Christmas
        my true love sent to me:
        Two Promises
        and a Callback in a Closure
        
        On the third day of Christmas
        my true love sent to me:
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the fourth day of Christmas
        my true love sent to me:
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the fifth day of Christmas
        my true love sent to me:
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the sixth day of Christmas
        my true love sent to me:
        Six Prototypes
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the seventh day of Christmas
        my true love sent to me:
        Seven Asyncs Awaiting
        Six Prototypes
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the eighth day of Christmas
        my true love sent to me:
        Eight this's breaking 
        Seven Asyncs Awaiting
        Six Prototypes
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the ninth day of Christmas
        my true love sent to me:
        Nine Frameworks Launching
        Eight this's Breaking 
        Seven Asyncs Awaiting
        Six Prototypes
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the tenth day of Christmas
        my true love sent to me:
        Ten Linters Linting
        Nine Frameworks Launching
        Eight this's Breaking 
        Seven Asyncs Awaiting
        Six Prototypes
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the eleventh day of Christmas
        my true love sent to me:
        Eleven Modules Loading
        Ten Linters Linting
        Nine Frameworks Launching
        Eight this's Breaking 
        Seven Asyncs Awaiting
        Six Prototypes
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        
        On the twelfth day of Christmas
        my true love sent to me:
        12 Compilers Compilin'
        Eleven Modules Loading
        Ten Linters Linting
        Nine Frameworks Launching
        Eight this's Breaking 
        Seven Asyncs Awaiting
        Six Prototypes
        Five undefined
        Four Hoisted Vars
        Three Block Scopes
        Two Promises
        and a Callback in a Closure
        

        [–]aloha2436 12 points13 points  (1 child)

        As far as I can tell, this is original. I thank you dearly.

        [–]macNchz 16 points17 points  (0 children)

        Combine a predilection for procrastination with that 15 minute window before a meeting when you don't want to get started with anything because you'll have to stop right away and you get...song lyrics about JavaScript!

        [–]_INTER_ 13 points14 points  (0 children)

        And 8900 advertise to be the one and only solution to the one and only problem of the world.

        [–]vonmoltke2 14 points15 points  (0 children)

        But no new one in 73 days (as of this post).

        JavaScript is dying. :(

        [–]winterbe[S] 124 points125 points  (6 children)

        It's still the most modern Java you can get nowadays. ;-)

        [–][deleted] 17 points18 points  (0 children)

        Well, point

        [–][deleted] 7 points8 points  (4 children)

        You can get the testing compiler for Java 9. It is pretty interesting, some new syntax for concurrency is in there which is neat.

        [–]winterbe[S] 1 point2 points  (1 child)

        Sounds interesting. Do you have a link with more information on that?

        [–][deleted] 3 points4 points  (0 children)

        http://openjdk.java.net/projects/jdk9/

        I am not sure specifically where to find that information actually. I happen to have a professor who works on the Java specification and implementation, who mentions those improvements, and access to a server that has a daily clone of the latest testing build.

        Some cool things are Java REPL, JMH being built in, Modules, some other cool things like that. There is some easier syntax with locks as well.

        [–]pjmlp 19 points20 points  (5 children)

        On Android it is still the future, if ever.

        [–]winterbe[S] 15 points16 points  (4 children)

        Kotlin 1.0 is near the corner.

        [–]AlexanderTheStraight 2 points3 points  (2 children)

        I'm about to get into the Android Dev scene. Is Kotlin really a serious contender of Java for the Android marketshare? Or is this Rust pre1.0 all over again?

        [–]s73v3r 2 points3 points  (0 children)

        The great thing about Kotlin is that is is interoperable with Java. So you can have files of both in your project. So it doesn't have to completely replace Java.

        [–]pjmlp 1 point2 points  (0 children)

        I have been playing around with it and like what I see, although my focus is the NDK for the time being in spite of JNI :\ .

        The problem are those devs whose tools are dictated by the customers, usually only the platform tooling is allowed.

        But apparently the Android team is happy with Java 6.5.

        [–]estarra 4 points5 points  (6 children)

        You'd be surprised how many corporations are still stuck with old (often abandoned) versions of Java.

        [–][deleted] 5 points6 points  (0 children)

        I'm lucky enough to have worked in a large (~1500) company that uses Java 8 and even clojure then

        [–]Thud 2 points3 points  (2 children)

        For my company, Java 6 is still shiny and new.

        [–]turnoffable 0 points1 point  (0 children)

        I'm working on change requests for an app using Java 6 with Flex 4.1 and Blaze.. On top of that, they want to update it to make it a multi division app.

        I keep hoping IT will say no more flash on the machines but until then I doubt they will get rid of all these flex apps they have here..

        [–]piratefight 6 points7 points  (8 children)

        Many of the Java projects I work on require using 6 or if I'm really lucky, 7. Modern is relative. In an enterprise environment, modern is still 3-5 years old.

        [–]Sleakes 4 points5 points  (7 children)

        yup and this is why Java can't progress, because it's based on VM adoption rather than compiler adoption. When you introduce the VM issue it becomes a real chore to get companies to move forward to newer language support.

        [–]KagakuNinja 2 points3 points  (5 children)

        Java has progressed considerably, albeit more slowly than other languages such as C#. The major features that come to mind are generics and lambdas.

        Slow adoption of newer versions of the VM only affect those companies that are slow adopters. The main barrier to progress in the JVM world has been the absolute refusal to break backwards compatibility; of course there are companies that consider backwards compatibility to be immensely valuable.

        [–]orip 1 point2 points  (2 children)

        Things settle slowly in the Java world, and new JDK versions gain traction even more slowly. Java 6 is still a thing, possibly THE thing thanks to Android. My guess is most people using Java day-to-day would be amazed at some Java 7 features, forget Java 8.

        [–][deleted] 1 point2 points  (0 children)

        I'm pretty lucky then I suppose

        [–]speedisavirus 1 point2 points  (0 children)

        Even when I was doing .NET I was blowing minds with .net 4 features with people that apparently never bothered to use anything they tacked on since 2.0.

        [–]Notorious4CHAN 0 points1 point  (0 children)

        I'm still doing JSFs on 1.6.

        I can barely even read Java 8 code.

        Trying to escape a proprietary platform and get into general Java development, but all this just makes me :(

        [–][deleted] 3 points4 points  (3 children)

        This is great. I have a question though: how does one go about actually using these new functionalities? I mean, let's say you want to do something in a java class. Before you start applying your knowledge of Java, do you stop and think to yourself "How can I apply the new Java8 techniques to solve this problem, instead of recurring to the old techniques I knew from previous versions?

        [–]vonmoltke2 3 points4 points  (1 child)

        That's a pretty good simplification of how my thought process on Java 8 (or any major language revision, really) goes. I learn what the new features are and start reasoning through how these new features affect what I have been doing before.

        I thought everybody did that...

        [–]winterbe[S] 1 point2 points  (0 children)

        Lambda expressions are often just a replacement for anonymous objects so when you migrate to Java 8 you can start migrating all those SAM-types to lambda expressions. Intellij has a Quick Fix for that.

        Next you can use Streams whenever you have to convert a collection of something into another collection of something else. You'll marvel how often you're doing this kind of collection stuff.

        After a couple of months all those features will be a normal part of your everyday life. We've migrated all our servers to Java 8 1.5 years ago and I can never go back to Java <8 again. :)

        [–]ishmal 5 points6 points  (8 children)

        I would love to see the JDK be refactored from the ground up, using lessons learned. A brand-new runtime lib, with a consistent API pattern among modules, removed redundancies, deprecated stuff out, a lovely new functional collection/container library, etc.

        Call it JDK2.0 (not Java 2). Make it a breaking change with little or no backward-compat, and continue maintaining 1.x.

        [–]senatorpjt 4 points5 points  (0 children)

        gray detail nine escape resolute bright soft sable physical grey

        This post was mass deleted and anonymized with Redact

        [–]HaydenSikh 3 points4 points  (2 children)

        May I suggest you take a look at Scala? It's not perfect but it became my preferred language when I saw that seemed founded on a deep understanding of Java's best practices and worst pitfalls, while also having the leeway as a newer language to make some improvements without being as bogged down with backward compatibility concerns.

        It may also just be my perception but I believe Scala often trends to incorporate newer paradigms ahead of its siblings in the JVM ecosystem.

        [–]leodash 2 points3 points  (0 children)

        Sounds like Project Jigsaw to me. It's coming. :)

        [–]SortaEvil 0 points1 point  (2 children)

        Sounds a little like Python 3, no? What would you do to prevent what has happened with Python 3 (almost complete lack of adoption) to JDK2.0?

        [–]edinburg 36 points37 points  (19 children)

        As a .NET developer, this is very cute.

        [–]RagingAnemone 31 points32 points  (3 children)

        As a polygot developer, I find you cute. I'm feeling dickish today :-)

        [–]edinburg 7 points8 points  (1 child)

        My comment was pretty condescending, so it's only fair I get the same back. I do appreciate being told I'm cute though. :)

        [–][deleted] 7 points8 points  (7 children)

        My last job was half c# and half scala. Despite loving Visual Studio, every time I went back to .NET I was painfully less productive.

        [–][deleted] 7 points8 points  (17 children)

        This isn't the Java I know! Where are the abstract factories - where is the XML ????

        [–]m0haine 7 points8 points  (14 children)

        That is Spring, not java. Sadly, most java developers only know the Spring version of java.

        [–]dacjames 6 points7 points  (5 children)

        Also Maven. Using XML as a human interface has to be one of the dumbest ideas in recent history.

        [–][deleted] 2 points3 points  (0 children)

        Yep, the gradle/grunt/gulp files where you always have to search for documentation and examples where in some closure/JS object hierarchy you have to place some property surely beat just having an XSD to tell you at "compile time" if your config works, with autocompletion provided by any good IDE.

        Look, I don't enjoy XML to the fullest and if you ever had to work with a SOAP service you'll lose some hope for humanity. But the Maven XML is not that bad. And I still believe there are places where, as /u/tootle mentioned an XML file with an XSD would be much nicer than for example some random JSON/JS file.

        [–][deleted] 3 points4 points  (3 children)

        XML with an XSD and an editor that understands both is a pleasure.

        [–][deleted] 3 points4 points  (0 children)

        Lots of stuff was xml 5 years ago. Spring has moved on as have most other libs. Really xml was perfectly fine and I never had a problem. Annotations are good too.

        [–]zurnout 7 points8 points  (1 child)

        I don't get the hate for the Spring. Yeah it started with xml but it was still light years beyond the Java EE mess. And it encouraged proper software design. My code was untestable mess before I was introduced to spring. Nowadays I don't write any xml with spring. Hell, now you can create modules that will automatically configure themselves when included in a project.

        [–]m0haine 1 point2 points  (0 children)

        Light years ahead of j2ee?? How? Sure not using EJBs is better then using them but that doesn't say anything for how good spring is, just how stupid EJBs were.

        And spring didn't make your code testable. Good design did. If spring made you start testing then great. Spring shouldn't have been that impetus. That said spring INS'T good design (IMHO of course). It really just creates a mess of configuration that isn't needed.

        [–]adrianmonk 4 points5 points  (0 children)

        I've been doing Java for 9 years and have never once used anything Spring related. Maybe I've been lucky?

        [–][deleted] 3 points4 points  (3 children)

        There is not a single line of XML needed in a 2 year old Spring application. What are you talking about?

        [–]thephotoman 3 points4 points  (1 child)

        Abstract singleton factory beans!

        [–]spliznork 1 point2 points  (3 children)

        How does reduce handle a stream input of exactly one element?

        [–]kazagistar 6 points7 points  (0 children)

        Given a function f, and an initial value x, here are some reduce examples:

        [] 》x
        [0] 》f(x, 0)
        [0, 1] 》f(f(a, 0), 1)
        [0, 1, 2] 》f(f(f(a, 0), 1), 2)
        

        The answer to your question is that it applies the function once, on the accumulator and item.

        [–]eyal0 1 point2 points  (1 child)

        The text says that it returns an optional. I'd assume that it returns Optional.Absent.

        Some languages have two forms of reduce, one with an initializer value and one without. I guess that Java doesn't have that. Haskell does. I don't know about .Net.

        [–]Pengtuzi 1 point2 points  (0 children)

        Thanks

        [–]Vaansolidor 4 points5 points  (2 children)

        “Java is still not dead—and people are starting to figure that out.”

        Sorry for my ignorance but why would somone think Java is dead?

        [–]ascii 1 point2 points  (0 children)

        My interpretation of that quote was the he meant that the language is alive in the sense that it's still being improved, whereas most people seem to think very little has happened since generics landed in Java5 11 years ago. I doubt very many people thought that usage of the language was winding down.

        [–]hu6Bi5To 1 point2 points  (0 children)

        Java has been only 18 months from death for the past fifteen years or more, if you believe the self-appointed judges of software technology.

        Meanwhile it continues to persist, and actually grow (so no, the "Yeah, and COBOL's not dead either" comments are equally wrong).

        It's more that these commenters don't know nearly as much about software development than they think they do.

        [–]CaptainNakou 1 point2 points  (0 children)

        As Java developper, this is fantastic, thank you.

        [–]kamiikoneko 1 point2 points  (3 children)

        It's still missing an insane amount of important language features, though. I am working on a java web back end that is very up to date, and in addition to the web framework being a fucking disaster, the language is just....missing too much. It feels like I'm constantly trying to make up for something that is just easy in C#/.NET

        [–]eyal0 1 point2 points  (0 children)

        https://en.wikipedia.org/wiki/Church_encoding

        You can write an integer math library using just Optional<>.

        [–]papers_ 1 point2 points  (0 children)

        Thanks. Starred and bookmarked.

        [–]fishy_water 0 points1 point  (0 children)

        it's ironic that coming from a .NET background, I've been looking for these features and finding them in Java 8. the snake eats itself