Java 13 New features
Java team has started releasing new versions and new features more frequently like any other programming languages like Angular. This fast moving releases will help java communities to be active even the other languages like kotlin and all taking the advantages.
Table of Contents
Java 13 – Release Date
September 17th, 2019 – Java 13 released and Java 14 is planned on March 17, 2020.
Java 13 New Features and Enhancements
- Byte Buffer
- Removal of javax.security package
- Switch expressions
- Yield keyword
- Additional Unicode supports
- Text blocks
Byte Buffer
- Byte Buffer and byte[] are not same.
- This Byte buffer is exists from Java 7, but new methods are introduced as part of Java 13.
Byte Buffer – New Java 13 feature Methods
- ByteBuffer get(int index, byte[] dist);
- ByteBuffer put(int index, byte[] src);
- ByteBuffer get(int index, byte[] dist, int offset, int length);
- ByteBuffer put(int index, byte[] src, int offset, int length);
Removal of javax.security package
javax.security.cert.Certificate and javax.security.cert.X509.Certificate packages are removed in Java 13.
javax.secuirty Migration Guideline:
javax.security should be changed to java.security. (only x removed from javax)
Before Java 13 | After Java 13 |
javax.security.cert.X509.Certificate | java.security.cert.X509.Certificate |
javax.security.cert.Certificate | java.security.cert.Certificate |
Switch Expressions
This Java 13 new feature – Switch expressions is preview feature from Java 12. Preview feature means it is just introduced as a test feature in java 12, if it is accepted in the prod releases then you can actually avail this additional stuff with switch expressions as part of java 13.
String siteNameBySiteRank = switch(siteRank) { case 1 -> "Google"; case 2 -> "Facebook"; case 3 -> "Amazon"; default -> "Unknown"; }
Yield Keyword in Java 13 as new feature (preview)
Yield keyword is also introduced as part of switch expressions only. Below code sample will explain the switch expressions with yield keyword.
String siteName = switch(rank){ case 1 -> { String site = "Google"; yield site; } case 2 -> { String site = "Facebook"; yield site; } default -> "unknown"; }
Purpose of Yield Keyword:
- Break is replaced with yield keyword in the switch expressions.
- Because, the switch expressions block can have any loops as well. In that case break will create a confusion whether to skip from this loop or from the switch case itself.
- So yield keyword comes into the picture.
- If you want to skip the loops, then use break, if you want to skip the switch case then use the yield keyword like above.
Unicde 12.1 Support
- Lot of new emoji characters, characters and scripts are introduced in java 13 as new features.
- Approximately, 60+ new emoji characters, 500+ new characters and few (I think 4) new scripts are introduced as a new feature in java 13.
Text Blocks – Choo Sweet Feature for Java Folks
- Multiple line string can be used as a text block. No more \n (or) append for multiple line strings.
- This starts and ends with three double quotes (“””YOUR_MULTI_LINE_STRING_GOES_HERE”””)
- If you print the text block then it just print like a string only.
Text Block – Java 13 Sample Program:
String address = """ { "Country":"India", "State":"Tamilnadu", "Pincode":"600044" } """;
Bonus:
Apart from above java 13 new features these are few things you can note down,
You don’t want to run javac HelloWorld.java and java HelloWorld going forward, only java HelloWorld. is enough. This internally runs the javac as well.
Since java is releasing new features every 6 months, they are planning many new features as preview features only. In order to run the preview feature, you need to must add –enable-preview while running it.
To run the java 13 text block preview feature you need to run something like this,
java --enable-preview --source 13 NgDeveloperTextBlockSample.java
You can also check the same in their official release notes.
Do you like to know how the hashing works internally in java ? then read here.