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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Coding
  3. Java
  4. Features in Java 10

Features in Java 10

Let's take a sneak peek int what's coming in Java 10, including local variable type inference and APIs for unmodifiable collections.

Gowtham Girithar Srirangasamy user avatar by
Gowtham Girithar Srirangasamy
·
Mar. 13, 18 · Tutorial
Like (16)
Save
Tweet
Share
58.85K Views

Join the DZone community and get the full member experience.

Join For Free

Java 9 has changed Java's structure by introducing modular development (Project Jigsaw). Meanwhile, Java 10 will add flavors by introducing new features of its own. A few features are explained below, with snippets.

Local Variable Type Inference

Java 7 introduced the diamond operator:

 List list=new ArrayList<>();  

But even though we have a diamond operator, there is still a lot of boilerplate code. So, Java 10 introduced var as a reserve type name to reduce verbosity. It can be used as a variable, method, and package name, but we cannot use it as a class or interface name.

The advantage is that we no need to explicitly declare the variable type, so it reduces boilerplate code and increases readability.

Image title

In the above example, we have used the var in various examples of local variable type inference.

Image title

The above example describes that we can use the var reserved keyword as a variable name and method names.

Image title

The above example describes that var is a restricted local variable type and cannot be used for class and interface names. That should be very helpful in reducing boilerplate code.

Image title

I personally don't prefer this type inference much due to cases where we are not sure which datatype it is implying.

Image title

The types are inferred at compiletime and not runtime, so it will not affect performance.The resulting bytecode is the same as that with an explicit type declaration.

Restrictions

We cannot use the var on a variable without initializers.

var i; // invalid


Also, var is not allowed in a compound declaration.

var i , j =0; //invalid


And we cannot initialize with a null value for var-type variables:

var i=null; // invalid


Optional.orElseThrow() Method

Java 10 introduced the new method orElseThrow to the Optional class. It is preferred as an alternative to the existing get method. An example below describes the working of the orElseThrow() method.

Image title


Removal of Runtime.getLocalizedInputStream and getLocalizedOutputStream Methods

Java 1o removed the getLocalizedInputStream and getLocalizedOutputStream methods from the Runtime class, as they had no uses with respect to an internalization mechanism.

Image title

APIs for Creating Unmodifiable Collections

List.copyOf, Set.copyOf, and Map.copyOf methods are added to create the instance from an existing instance. 

Example:

Image title

Also, toUnmodifiableList, toUnmodifiableSet, and toUnmodifiableMap have been added to the Collectors class in the Stream package, which will produce unmodifiable collections.

Example:

Image title

Byte Code Generation for Enhanced For Loops

The following example describes the generation of the following lines of code until Java 9.

 List data = new ArrayList<>();for (String b : data);  

Image title

In Java 10, iterator variables are declared outside for loops and initialized to the null value immediately once the operation is over, so GC can get rid of unused memory.

{
    Iterator iterator = data.iterator();
    for (; iterator.hasNext();) 
    {
        String b = (String)iterator.next();
    }
    b = null;
    iterator = null;
} 


Relax the Removal of Finalize Methods in FileInputStreams and FileOutputStreams

The resources used by the stream should be cleaned by calling the close method or using the try with resources feature. In the future, classes that extend the FileInputStream or FileOutputStream should modify the cleanup approach such as Cleaner instead overriding the finalize method.

Removal of Deprecated Methods

The deprecated java.security.{Certificate, Identity, IdentityScope, Signer} classes are marked for removal by setting the value of forRemoval=true in a Deprecated annotation.

The deprecated java.security.acl is also now marked for removal by the setting the value of forRemoval=true in a Deprecated annotation. A few fields and methods in java.lang.SecurityManager are also marked for removal in this version.

javax.security.auth.Policy is also marked for removal and features of the class are now available in java.security.Policy.

com.sun.security.auth.** classes, which were marked for removal in JDK 9 by setting the value of forRemoval =true in a Deprecated annotation, have been removed

Parallel Full GC for G1

Java 9 made G1 as the default, and in Java 10, parallel full Garbage Collection support for G1 will improve the worst-case latencies. It parallelized the mark-sweep-compact algorithm.

The number of threads in parallel can be controlled by the XX:ParallelGCThreads option.

Removal of javah  

The native header tool is now removed and we can generate the native header using the Java compiler with the -h option

All the code is executed using JShell.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT Prompts for Agile Practitioners
  • Top 5 Node.js REST API Frameworks
  • Top 10 Secure Coding Practices Every Developer Should Know
  • Load Balancing Pattern

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: