DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Trending

  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Designing a Java Connector for Software Integrations
  • Mastering Advanced Aggregations in Spark SQL
  1. DZone
  2. Coding
  3. Java
  4. Java 17 for the Impatient

Java 17 for the Impatient

Learn the basics of Java 17.

By 
Abhishek Giri user avatar
Abhishek Giri
·
Jan. 08, 22 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
14.2K Views

Join the DZone community and get the full member experience.

Join For Free

I have been working on Java 8 for a long time and, now, Java 17 has been released, so let's have a quick look at this article which focuses on the latest Java 17 LTS features. 

There are lots of features and improvements that have been introduced, however, in this article, we will focus on a limited set:

  1. Record classes
  2. Sealed Class
  3. Switch case
  4. Enhanced instanceOf Operator
  5. Compact Number Format
  6. Text Blocks
  7. Stream toList

Records Classes

Records classes are immutable and used as data transfer objects with zero boilerplate code. Record classes are defined with the record keyword. It always extends the java.lang.Record class. 

record Country(String name,String capital){}

The above line of code generates functionality automatically like

  • All fields are private and final.
  • A canonical constructor for all fields.
  • Public accessor for all fields.
  • equals, hashcode, and toString methods.

Also, it can be defined locally either inside a class or method.

Java
 
public class SampleRecordClass {
    record Country(String name,String capital){}
    public static void main(String[] args) {
        Country firstCountry = new Country("Netherlands","Amsterdam");
        Country secondCountry = new Country("Germany","Berlin");
        System.out.println("First country object is " + firstCountry);
        System.out.println("Second country object is " + secondCountry);
        System.out.println("Check if both objects equal" + firstCountry.equals(secondCountry));
        // Copying object using attribute method of existing records object
        Country countryClone = new Country(firstCountry.name,firstCountry.capital);
        System.out.println("country clone object" + countryClone);
    }
}


Sealed Class

A sealed class specifically lists the permitted direct subclasses. It won’t allow any other class to extend.

Java
 
public sealed class SampleSealedClassApp permits SampleSealedApp{

}


Though the permits keywords can be removed if all the classes are defined in the same file.

 
public sealed class SampleSealedClassApp  {
final class SampleClass extends SampleSealedClassApp{}
final class SampleSecondClass extends SampleSealedClassApp{}
}


Switch Case

The switch statement in Java has evolved over time. At first, it allowed only primitives then it introduced String. In Java 17, we have the pattern matching of instanceOf with a switch case, which allows us to pass different types of complex objects.

The switch statement can now be written like this

Java
 
public static void onShow(Country country) {
        switch (country) {
            case NETHERLANDS, POLAND, GERMANY -> System.out.println("European Country");
            case INDIA, BHUTAN, NEPAL -> System.out.println("Asian Country");
            default -> System.out.println("It's from Wakanda");
        }
    }


Switch Statement With Yield

Another way to write switch cases is with blocks where we can add logic inside the block:

Java
 
public static void onShowWithYield(Country country) {
        String output = switch (country) {
            case NETHERLANDS, POLAND, GERMANY -> {
                System.out.println("European Country" + country);
                yield "Country belongs to Europe Continent";
            }
            case INDIA, BHUTAN, NEPAL -> {
                System.out.println("Asian Country" + country);
                yield "Country belongs to Asian Continent";
            }
            default -> "It's from Wakanda";
        };
        System.out.println(output);
    }


Switch Statement With the Return

Also, the switch statement can now return a value, written like this:

Java
 
public static void onShowWithReturnValue(Country country) {
        String output = switch (country) {
            case NETHERLANDS, POLAND, GERMANY -> "European Country";
            case INDIA, BHUTAN, NEPAL -> "Asian Country";
            default -> "It's from Wakanda";
        };
        System.out.println(output);
    }


Switch Statement With Type Coverage

In Java 17, we can also check for the type of selectors as sealed class as we have seen above. It does not need a default case because it does the type coverage with all permitted classes.

Java
 
sealed interface SampleSealed permits ClassOne, ClassTwo, ClassThree {
    }

    final class ClassOne implements SampleSealed {
    }

    final class ClassTwo implements SampleSealed {
    }

    record ClassThree(String name) implements SampleSealed {
    }

    static String withSealedClass(SampleSealed sampleSealedClassApp) {
        return switch (sampleSealedClassApp) {
            case ClassOne classOne -> "Sample class one";
            case ClassTwo classTwo -> "Sample class two";
            case ClassThree classThree -> "Sample class three";
        };

    }


In case it does not cover all the possible values, then the compiler will throw an error. As we can see in the example below, the ClassOne type is missing, so it will generate a compile-time error.

Java
 
static String withSealedClass(SampleSealed sampleSealedClassApp) {
        return switch (sampleSealedClassApp) { //error - Switch statement doesn't cover all possible value
            case ClassTwo classTwo -> "Sample class two";
            case ClassThree classThree -> "Sample class three";
        };


Enhanced instanceOf Operator

An enhanced instanceOf operator allows you to pattern match and eliminates the extra line of code that was required earlier to perform casts:

Java
 
private static void patternMatchingInstanceOf() {
        Object o = new CountryData("Netherlands","Amsterdam","Europe");
        if (o instanceof CountryData countryData) {
            System.out.println("This capital of Netherlands is " + countryData.capital());
        }
    }


In the above snippet, the scope of the variable countryData is specific to the if block. The variable in scope can be used to match the pattern strictly like this:

Java
 
private static void patternMatchingInstanceOfScope() {
        Object o = new CountryData("Netherlands","Amsterdam","Europe");
        if (o instanceof CountryData countryData && countryData.continent().equals("Europe")) {
            System.out.println("This continent of Netherlands is " + countryData.continent());
        }
    }


It is important to keep in my mind that the variable inside the scope should not be ambiguous. The condition && above can only be executed successfully after checking the result of instanceOf the results as true. If we change the && to ||, the code will not compile.

Compact Number Format

The CompactNumberFormat class is a subclass of NumberFormat class in the java.text package. It is responsible for formatting a number in compact form. A factory method is added to NumberFormat to format numbers in compact, human-readable form. There are two styles of formats, LONG and SHORT, as shown below

Java
 
NumberFormat numberFormatLong = NumberFormat
                .getCompactNumberInstance(Locale.forLanguageTag("NL"), NumberFormat.Style.LONG);
        System.out.println(numberFormatLong.format(2000));
        System.out.println(numberFormatLong.format(20000));
        System.out.println(numberFormatLong.format(200000));


The output of the above code will look like this:

 
2 duizend
20 duizend
200 duizend


Java
 
 NumberFormat numberFormatShort = NumberFormat
                .getCompactNumberInstance(Locale.forLanguageTag("NL"), NumberFormat.Style.SHORT);
        System.out.println(numberFormatShort.format(2000));
        System.out.println(numberFormatShort.format(20000));
        System.out.println(numberFormatShort.format(200000));


The output of the above code will look like this:

 
2K
20K
200K


Text Blocks

Text Blocks are used to make code more readable. It allows us to write multi-line strings. A Text Block is started with three quotes followed by the line break and closed by three quotes as shown below:

Java
 
public static void main(String[] args) {
        String multiLineText = """
                There are good ships and wood ships,
                ships that sail the sea,
                but the best ships are friendships,
                may they always be!
                """;

        System.out.println("The multi line text example" + multiLineText);
    }


Streams toList

Previously, to convert a stream to a list, we needed to use the collect method with Collectors.toList(). But now, in Java 17, we can directly call a toList() method on stream object as seen below:

Java
 
private static void countryStreamToList() {
        Stream<String> countryStream = Stream.of("Germany", "Georgia", "Bulgaria");
        List<String> countryList = countryStream.toList();
        for (String country : countryList) {
            System.out.println(country);
        }
    }


These are the must-know features for Java developers in their daily work. In the next blog, we will look at other performance improvement features introduced in Java 17 LTS.

Until then, happy coding.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Merge HTML Documents in Java
  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Using Python Libraries in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!