Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Switch Expressions coming to Java 12  RSS feed

 
Saloon Keeper
Posts: 2471
317
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A new feature targeted for Java 12 includes enhancements to the switch statement so it can be used an expression:
For complex expressions, code blocks can be specified:
To learn about this proposed new feature and other enhancements to switch, see JEP 325: Switch Expressions
 
Sheriff
Posts: 21686
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may still be an experimental feature, which means it needs to be enabled at both compile and run time. But yes, project Amber is coming up with a lot of interesting stuff.

For those wondering why Ron's first example doesn't need a default clause - that's because the compiler can tell that all enum constants are present in the switch (there will be an exception if the enum has extra constants added without recompiling this class).


One other enhancement from Amber I'm looking forward to is JEP 305. Basically this means you can combine instanceof and assignment into one:


Unfortunately, JEP 326 (raw string literals) has been dropped from Java 12, because of the ambiguity of what `` could mean - an empty string literal, or the start of a string literal. I was quite disappointed by that decision...
 
Rob Spoor
Sheriff
Posts: 21686
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A link for project Amber: https://openjdk.java.net/projects/amber/
 
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:. . . the compiler can tell that all enum constants are present in the switch . . .

Very interesting; that might correct the problem we are discussing here and in subsequent posts.
 
Saloon Keeper
Posts: 2182
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am happy about the variable scoping side effect.
And yes, breaks are too verbose !
 
Rancher
Posts: 3111
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I STILL miss my BBC Basic 5 switch:

and then a series of boolean expressions, gosh that was handy from time to time...
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!