3 ms·
The new switch feature looks nice, the way it previously used to work was so unwieldy.
by connorlwilkes 8y ago
The new switch feature looks nice, the way it previously used to work was so unwieldy.
- azhenley 8y agoIn 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); }
- jason0597 8y agoReminds me of Kotlin's "when" statement
- _old_dude_ 8y agoor 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; });
- melling 8y agoThat'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.
- andystanton 8y agoLocal 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-inference https://developer.oracle.com/java/jdk-10-local-variable-type... 2. https://docs.oracle.com/javase/9/docs/api/java/util/List.html#of-- https://docs.oracle.com/javase/9/docs/api/java/util/List.htm...
- spricket 8y agoAs 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
- jacques_chester 8y agoThe .parallel() method in Java Streams is basically one of my favourite magic spells ever.
- spricket 8y agoAh yes, parallel stream(), for when you need to filter a list of 13000 entries for.... Reasons. Bad reasons. Use it all the time :)
- jacques_chester 8y agoI 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!
- spricket 8y agoYeah 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
- ars 8y agoI assume the expression becomes null if none of those match?
- _old_dude_ 8y agoNo my bad, it doesn't compile, you have to add a default.
- Freak_NL 8y agoThe number of times I've wished this was possible is probably 50% of the times I've used a switch statement.
- gsu 8y agoswitch assignment: int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; };