Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Your code on the Fastlane: catch up with everyone

Your code on the Fastlane: catch up with everyone

Moving your project to the latest Java can mean faster code, cheaper cloud bills, and many more. Up to 25% of heap memory can be saved just by using Java 11. Plus, if you write your code to the latest Java, it will become clearer, concise, and easy to read, which means less chance to produce bugs.

In this short talk, I will demonstrate a few small and easy steps to upgrade your knowledge and Java code to the latest version.

A N M Bazlur Rahman

February 21, 2023
Tweet

More Decks by A N M Bazlur Rahman

Other Decks in Programming

Transcript

  1. Your code on the Fastlane: let’s catch up with everyone

    Jakarta EE Ambassador, Java Champion, Author, Blogger, Speaker 🐦@bazlur_rahman 1
  2. 3 Java: The Latest Buzz Java has improved tremendously and

    added tons of new features recently since java 8
  3. Reclaim 25% of Heap Memory and Reduce Cloud Bills •

    In Java 8 and pre-Java 8, a char array is used in a String, taking up 16 bits of memory per character. • Java 11 introduces compact strings, which use an 8-bit byte array to encode a string, unless they explicitly need 16-bit characters. • This means that the size of an average string in Java 11 is roughly half the size of the same in Java 8. • As a result, upgrading to Java 11 and using compact strings can help you reclaim 25% of heap memory, and reduce cloud bills. 4
  4. New Features You Need to Know • Records (JEP 359)

    • Pattern Matching for instanceof (JEP 305) • Text Blocks (JEP 378) • Switch Expressions (JEP 361) • Improved Type Inference (JEP 286) • Convenience Factory Methods for Collections (JEP 269) • Sealed Classes (JEP 409) • Optional classes • Virtual Threads (JEP 425) 5
  5. Introducing Java Records • Java records are a new kind

    of class that are compact, immutable, and transparent containers for data. • With records, you can declare the state and behavior of your objects in a single line of code, using concise and expressive syntax. • Records provide built-in implementations for equals(), hashCode(), and toString(), reducing the boilerplate code and the risk of errors. • Records can also help you write better APIs, by providing a clear and consistent representation of your data types. • Records are easy to use and integrate with existing Java code, and they are fully compatible with serialization and deserialization. 6
  6. 7

  7. 8

  8. Limitations 1. Records are immutable: Once created, the values of

    a record cannot be modified. 2. Limited expressiveness: Records are designed to be simple and concise, so they have limited expressiveness compared to regular classes. For example, you cannot define custom constructors or instance initialization blocks in a record. You cannot declare any instance field in a record. You cannot add any instance field that would not correspond to a component. 3. Limited inheritance: Records can extend other classes or interfaces, but they cannot be extended themselves. This means that you cannot create a subclass of a record. 4. Limited usage: Records are only available in Java 16 and later versions, so they cannot be used in older Java projects. Additionally, not all Java frameworks and libraries may support records yet, which can limit their usage in certain contexts. 5. Potential performance impact: Because records are immutable, creating a new record instance requires creating a new object, which can have a performance impact in certain scenarios. 9
  9. 10

  10. Exploring Pattern Matching for instanceof • Pattern Matching for instanceof

    simplifies testing object types and casting them in a single expression, replacing complex if-else chains and type casts with a concise syntax. • It supports various patterns, improves code readability by reducing the need for intermediate variables, and is compatible with generics and inheritance. • Pattern Matching for instanceof can also aid in writing better APIs by providing a clear representation of your type hierarchy and relationships. 11
  11. 15

  12. 16

  13. Mastering Pattern Matching for switch • Allows you to match

    complex patterns and expressions in a switch statement, improving the expressiveness and flexibility of your code. • You can replace nested if-else chains and complex boolean expressions with a simpler and more intuitive syntax, based on pattern matching and deconstruction. • Allows you to extract values from the matched pattern or expression in the same statement, reducing the need for intermediate variables and improving the readability of your code. 17
  14. Exploring Record Patterns • Record patterns use concise syntax to

    match and extract fields from records in a switch statement, replacing complex if-else chains and object casts with a simpler and more intuitive syntax based on pattern matching and deconstruction. • They provide a clear representation of record types and are fully compatible with other pattern types, such as type patterns and null patterns. • Record patterns aid in writing better APIs, providing a consistent representation of data types and their behavior. • They are easy to use, integrate well with existing Java code, and are fully compatible with serialization and deserialization. 19
  15. 21

  16. 22

  17. 24 Exploring Switch Expression • Switch Expressions provide a concise

    and expressive alternative to complex if-else chains and nested switch statements, reducing code size and errors. • They support pattern matching, multi-line blocks, and return statements, and are compatible with other Java features like lambda expressions and try-with-resources statements. • Switch Expressions offer better type checking and error handling with exhaustive and type-safe checks and clear error messages. • They're easy to use, integrate with existing code, and support a range of scenarios like API design, data processing, and UI programming.
  18. Simplifying Strings with Java Text Blocks • Simplifies creation of

    multiline strings. • Allows inclusion of line breaks and whitespace without escape characters or concatenation. • Strips leading and trailing whitespace from each line by default. • Preserves line breaks and whitespaces in the text. • Supports escaping double quotes with "". • Introduced in Java 13. 26
  19. Introducing Local Type Inference in Java • Local Type Inference

    uses the 'var' keyword to declare local variables without specifying their types explicitly, reducing verbosity and repetition. • It improves code readability and maintainability by emphasizing variable intention and reducing noise and distraction. • Local Type Inference is fully compatible with other Java features • It improves productivity and creativity by enabling more concise and expressive code, allowing for easier experimentation and iteration. 29
  20. Principle & Guideline P1. Reading code is more important than

    writing code. P2. Code should be clear from local reasoning. P3. Code readability shouldn’t depend on IDEs. P4. Explicit types are a tradeoff. Reference: https://openjdk.org/projects/amber/guides/lvti-style-guide 32
  21. 33

  22. 34

  23. Convenience Factory Methods for Collections • It provides a cleaner

    and more readable syntax for creating and initializing collections in a single line. • They offer a wider range of options and styles than traditional constructors or builders and are fully compatible with other Java features like streams and lambdas. • Convenience Factory Methods can improve productivity and creativity, enabling more concise and expressive code and freeing from boilerplate code, and useful in scenarios like data manipulation, testing, and API design. 35
  24. Exploring Sealed Classes in Java 1. Java 15 introduces sealed

    classes to restrict class inheritance. 2. Sealed classes allow a set of specific classes to inherit from a class or interface. 3. Sealed classes improve code readability, maintainability, and security. 4. They provide more flexibility than the final keyword in specifying a fixed set of subtypes. 5. Sealed classes enhance API design by making the class hierarchy more self-contained. 6. They prevent unintended inheritance and reduce the risk of bugs and security vulnerabilities. 37
  25. 38

  26. 39

  27. 41

  28. 43

  29. 44

  30. Exploring Virtual Threads in Java • Virtual Threads are lightweight

    threads that improve concurrency scalability and efficiency by reducing native thread overhead and contention. • They provide a safer and more flexible concurrency model and can improve application resilience and availability. • Virtual Threads are easy to use, integrate with existing Java code, and are compatible with other Java features like streams and completable futures. 46