This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Switch Expressions coming to Java 12  RSS feed

 
Saloon Keeper
Posts: 2353
293
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: 21603
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: 21603
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: 62881
203
  • 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: 2146
80
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 !
 
Master Rancher
Posts: 3080
108
  • 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...
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!