News for hackers. Stuff that matters.
Re:Java 12 by connorlwilkes on Tuesday March 19, 2019 @04:54PM (#19435742)
The new switch feature looks nice, the way it previously used to work was so unwieldy.
Re:Java 12 by azhenley on Tuesday March 19, 2019 @05:05PM (#19435868)
In case someone is curious what it looks like with the new -> form:

    switch (day) {
        case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
        case TUESDAY                -> System.out.println(7);
        case THURSDAY, SATURDAY     -> System.out.println(8);
        case WEDNESDAY              -> System.out.println(9);
    }
Re:Java 12 by _old_dude_ on Tuesday March 19, 2019 @05:11PM (#19435930)
or using the switch as an expression

  System.out.println(switch(day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
    });
Re:Java 12 by Freak_NL on Wednesday March 20, 2019 @04:11AM (#19439974)
The number of times I've wished this was possible is probably 50% of the times I've used a switch statement.
Re:Java 12 by melling on Tuesday March 19, 2019 @05:15PM (#19435966)
That's much nicer. Anything else added to Java recently to reduce the boilerplate? I haven't used Java much in 5-6 years. Glad they are iterating faster.
Re:Java 12 by andystanton on Tuesday March 19, 2019 @05:34PM (#19436198)
Local type inference[1] means you can write:

  var foo = new Map<String, String>();
The static initialisation methods on collections[2] let you write

  var foo = List.of(1, 2, 3);
It's still clunkier than many newer languages but it is improving.

1. https://developer.oracle.com/java/jdk-10-local-variable-type...

2. https://docs.oracle.com/javase/9/docs/api/java/util/List.htm...

Re:Java 12 by spricket on Tuesday March 19, 2019 @09:07PM (#19437901)
As another poster mentioned, the new type inference is great. So is lambda support. Another is Lombok.... Yeah it's not official but we use it literally everywhere and it helps a ton with data classes especially.

Nullaway and a bunch of other linters will also save you from accidentally falling in one of the dusty corners.

RxJava and Java Streams are also very nice.

Anonymous classes are nice too I guess (although they're fairly limited).

Overall the java experience has improved massively in the last 5 years, but you need to know where to look. Highly recommend the two popular "awesome java" repos on GitHub for an overview

Re:Java 12 by jacques_chester on Tuesday March 19, 2019 @09:13PM (#19437931)
The .parallel() method in Java Streams is basically one of my favourite magic spells ever.
Re:Java 12 by spricket on Tuesday March 19, 2019 @09:16PM (#19437949)
Ah yes, parallel stream(), for when you need to filter a list of 13000 entries for.... Reasons. Bad reasons.

Use it all the time :)

Re:Java 12 by jacques_chester on Tuesday March 19, 2019 @09:23PM (#19437993)
I tried it once on a simple monte carlo thingy and legitimately said "wow!" out loud when it just worked.

God, all the fussing with threads and fork-joins and mapreduces and god knows what everyone else wanted me to do: gone. Gone!

Re:Java 12 by spricket on Tuesday March 19, 2019 @09:41PM (#19438095)
Yeah seriously Java's threading support is good but the boilerplate was horrific.

One thing you should know is that parallel streams use default thread pool unless you specify otherwise. Had that choke up some REST handler threads a few times. If you think it might be an issue make sure to specify a different pool

Re:Java 12 by ars on Tuesday March 19, 2019 @05:19PM (#19436029)
I assume the expression becomes null if none of those match?
Re:Java 12 by _old_dude_ on Tuesday March 19, 2019 @05:26PM (#19436093)
No my bad, it doesn't compile, you have to add a default.
Re:Java 12 by gsu on Tuesday March 19, 2019 @06:52PM (#19436952)
switch assignment:

  int numLetters = switch (day) {
      case MONDAY, FRIDAY, SUNDAY -> 6;
      case TUESDAY                -> 7;
      case THURSDAY, SATURDAY     -> 8;
      case WEDNESDAY              -> 9;
  };
Re:Java 12 by jason0597 on Tuesday March 19, 2019 @05:08PM (#19435901)
Reminds me of Kotlin's "when" statement