Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Java 11 and 12 ??? New Features
Java 11 and 12 ??? New Features

Java 11 and 12 ??? New Features: Learn about Project Amber and the latest developments in the Java language and platform

Arrow left icon
Profile Icon Gupta
Arrow right icon
€18.99 per month
Paperback Mar 2019 242 pages 1st Edition
eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Gupta
Arrow right icon
€18.99 per month
Paperback Mar 2019 242 pages 1st Edition
eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Java 11 and 12 ??? New Features

Type Inference

The ability to use type inference with local variables (var) is one of the star features of Java 10. It reduces the verbosity of the language without compromising Java's dependable static binding and type safety. The compiler infers the type by using the information available in the code, and adds it to the bytecode that it generates.

Every new concept has its own set of benefits, limitations, and complexities. Using type inference with var is no exception. As you work through this chapter, using var will enthrall and frustrate you, but you will emerge triumphantly.

In this chapter, we'll cover the following topics:

  • What is type inference?
  • Type inference with var
  • Dos and don'ts of working with var
  • Type inference versus dynamic binding

What is type inference?

Imagine solving a riddle, such as the one shown in the following image, with multiple constraints in the form of hints. You resolve the constraints to derive the answer. You can compare type inference to generating constraints and then resolving them, in order to determine the data types in a programming language. Type inference is the capability of the compiler to determine the type of the data, by using the information that is already available in the code—literal values, method invocations, and their declarations. For a developer, type inference reduces verbosity, as indicated by the following diagram:

For your reference, the answer to the preceding riddle is 87 (just turn the image upside down, and you'll find the numbers in a sequence).

Type inference is not new to Java. It has been taken to the next level with the introduction of var...

Type inference with var

The following lines of code show how local variables (and all other variables) were defined prior to Java 10:

String name = "Java Everywhere"; 
LocalDateTime dateTime = new LocalDateTime.now();

Starting with Java 10, by using var, you can drop the mandatory explicit type in the declaration of local variables, as follows:

var name = "Java Everywhere";            // variable 'name' inferred as 
// String var dateTime = new LocalDateTime.now(); // var 'dateTime' inferred as
// LocalDateTime

Does it look like the preceding code doesn't offer a lot of benefits? Imagine you could take the following code:

HashMap<Integer, String> map = new HashMap<Integer, String>(); 

And replace it with this code, instead:

var map =...

Type inference in previous versions of Java

Although var takes inference to a new level in Java 10, the concept of type inference existed in Java's previous versions. Let's look at some examples of type inference in the previous versions of Java.

Type inference in Java 5

Generics introduced a type system to enable the developers to abstract over types. It restricted a class, interface, or method to working with instances of specified types, providing compile type safety. Generics were defined to add compile type safety to the Collections framework. Generics enable programs to detect certain bugs during compilation, so they can't creep into the runtime code.

Java used type inference for generic method type arguments...

Challenges

The use of var doesn't come without its share of challenges, for both the developers of the Java language and its users. Let's start with the reasons why var has limited usage.

Limiting the scope of failed assumptions

As you know, the use of var types is limited to local variables in Java. They are not allowed in the public API, as method parameters or as the return type of methods. Some languages support type inference for all types of variables. Java may allow us to do so in the future, but we don't know when.

However, there are strong reasons for limiting the scope of the inferred variables, to spot the errors due to mismatch of assumptions and the actual, early on. The contracts of the public...

Type inference versus dynamic binding

The use of type inference with var isn't pushing Java towards the dynamic binding domain. Java is still a strongly-typed static language. The type inference in Java is syntactic sugar. The compiler infers the type and adds it to the bytecode. In dynamic binding, a variable type is inferred at runtime. This can lead to more errors being discovered later.

Summary

In this chapter, we covered local variable inference, or var, as introduced in Java 10. The var type enables you to drop the explicit data type for a local variable in a method. We covered the various dos and don'ts for the usage of var. Limited to local variables, variables defined using var must be initialized with a value. They can be used with all types of variables—primitives and objects. Variables defined with var can also be passed to methods and returned from methods; method declaration compatibility rules apply.

To avoid risking your type safety with generics, ensure that you pass relevant information when using var with generics. Although it doesn't make a lot of sense, the use of explicit casting is allowed with variables defined using var.

We also covered ways in which type inference existed in previous versions of Java (5, 7, and 8). Toward...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Overcome the challenges involved in migrating to new versions of Java
  • Discover how Oracle has bridged the gap between Java and native code
  • Make the best use of new Java features and libraries in your applications

Description

With its new six-monthly release cadence, Java is moving forward faster. In addition to planned version releases, a lot of work is currently being undertaken on various Java projects at Oracle. In order to make best use of the new features in their applications and libraries, you must be well-versed with the most recent advancements. Java 11 and 12 – New Features will take you through the latest developments in Java, right from variable type inference and simplified multithreading through to performance improvements, which are covered in depth to help you make your applications more efficient. This book explains the relevance and applicability of Java's new features, and answers your questions on whether to invest in migrating to new Java versions and when to migrate. You'll also get to grips with platform features, such as AppCDS and new garbage collectors, to tune and optimize your application—from reduced launch time and latency to improved performance and throughput. By the end of this book, you will be equipped with a thorough understanding of the new features of Java 11, 12, and Project Amber, and possess the skills to apply them with a view to improving your application's performance.

Who is this book for?

If you’re an executive or solutions architect responsible for technology selection or Java migration decisions, this Java book is for you. You’ll also benefit from this book if you’re a computer science enthusiast curious to learn about the latest and upcoming Java features. This book will help you migrate your solutions from Java 8 or older to the latest Java release.

What you will learn

  • Study type interference and how to work with the var type
  • Understand Class-Data Sharing, its benefits, and limitations
  • Discover platform options to reduce your application's launch time
  • Improve application performance by switching garbage collectors
  • Get up to date with the new Java release cadence
  • Define and assess decision criteria for migrating to a new version of Java

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 26, 2019
Length: 242 pages
Edition : 1st
Language : English
ISBN-13 : 9781789133271
Vendor :
Oracle
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Mar 26, 2019
Length: 242 pages
Edition : 1st
Language : English
ISBN-13 : 9781789133271
Vendor :
Oracle
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 86.97
Mastering Microservices with Java
€36.99
Java 11 and 12 ??? New Features
€24.99
Java Fundamentals
€24.99
Total 86.97 Stars icon
Banner background image

Table of Contents

22 Chapters
Section 1: JDK 10 Chevron down icon Chevron up icon
Type Inference Chevron down icon Chevron up icon
AppCDS Chevron down icon Chevron up icon
Garbage Collector Optimizations Chevron down icon Chevron up icon
Miscellaneous Improvements in JDK 10 Chevron down icon Chevron up icon
Section 2: JDK 11 Chevron down icon Chevron up icon
Local Variable Syntax for Lambda Parameters Chevron down icon Chevron up icon
Epsilon GC Chevron down icon Chevron up icon
The HTTP Client API Chevron down icon Chevron up icon
ZGC Chevron down icon Chevron up icon
Flight Recorder and Mission Control Chevron down icon Chevron up icon
Miscellaneous Improvements in JDK 11 Chevron down icon Chevron up icon
Section 3: JDK 12 Chevron down icon Chevron up icon
Switch Expressions Chevron down icon Chevron up icon
Miscellaneous Improvements in JDK 12 Chevron down icon Chevron up icon
Section 4: Project Amber Chevron down icon Chevron up icon
Enhanced Enums in Project Amber Chevron down icon Chevron up icon
Data Classes and Their Usage Chevron down icon Chevron up icon
Raw String Literals Chevron down icon Chevron up icon
Lambda Leftovers Chevron down icon Chevron up icon
Pattern Matching Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.