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
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Coding
  3. Java
  4. Java 9: A Look at Milling Project Coin

Java 9: A Look at Milling Project Coin

Follow along this explanation of JEP 213: Milling Project Coin. Check out the tweaks coming to the Java language for identifier names, SafeVarargs, and more.

Gowtham Girithar Srirangasamy user avatar by
Gowtham Girithar Srirangasamy
·
Jul. 21, 17 · Tutorial
Like (5)
Save
Tweet
Share
11.02K Views

Join the DZone community and get the full member experience.

Join For Free

In this section, we are going to discuss the new Java 9 enhancements as part of JEP 213: Milling Project Coin. The purpose of JEP 213 is to address some rough edges. Milling Project Coin has five small amendments to the Java programming language.

1. Underscore as an Identifier Name

In Java 8, an underscore ( _ ) is allowed as an identifier name, but the compiler will show a warning that "It will not be supported after Java SE 8." So Java 9 completed the removal of underscore from the set of legal identifier names.

Java 8: Warning for _ as the identifier name

Image title

Java 9: Error for _ as the identifier name

Image title

The underscore will be used as a keyword for an unnamed lambda parameter in future Java releases (JEP 302).That's the reason the compiler stated "_" is a keyword while compiling the Java file.

2. Enhancement of try-with-resources Statements

Java 7 introduced the try-with-resources statement, where resources will be closed automatically after the execution. It requires an additional variable for the resources to be assigned. But Java 9 manages the same with the final or effectively final variables. The effectively final variable is the variable or the parameter whose values will never be changed once it is initialized.

Example: int num=10; 

The above num is treated as effectively final by the compiler until you change the value, so the effectively final variable is treated like the final variable unless it is changed.

Java 7: Requires the additional variable:

InputStream inputStream = new FileInputStream("test.txt");
try (InputStream stream = inputStream) {} catch (IOException e) {}


Java 9: Doesn't require additional variables:

InputStream inputStream = new FileInputStream("test.txt");
try (inputStream) {} catch (IOException e) {}


3. Private Methods in the Interface

Please refer my previous post on Java 9 Interface for this section.

4. SafeVarargs to Support Private Methods

Java 7 introduced the @SafeVarargs annotation to final, static methods, and constructors. Java 9 extends this functionality to use for private methods, too.

Example:

import java.util.Arrays;
import java.util.List;
public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.printList(Arrays.asList("Apple", "Bat"));
    }
    private void printList(List < String > ...stringList) {
        for (List <String> list: stringList)
            System.out.println(list);
    }
}


Upon compiling the above code with Java 8 using the javac tool with -Xlink:unchecked to get a detailed warning, it throws two warnings, as seen below:

Image title

We can avoid this warning message by annotating the private method with the @SafeVarargs annotation in Java 9. Compile the same code by annotating the printList with @SafeVarargs.

Image title

5. Diamond Operators With Anonymous Classes

Java 7 introduced the diamond operator ( <> ) in generic class instantiation contexts. The following code shows its use:

List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<>();


list1 is the pre-Java 7 approach to instantiate a generic class. The actual type argument is specified while instantiating it. But list2 is instantiated without mentioning the actual type argument, which was introduced in Java 7.

But in Java 7, we cannot use the diamond operator in anonymous classes. Java 9 enhanced the type inference algorithm to tell whether the inferred type is denotable when analyzing an anonymous class that supports the diamond operator.

Example:

Iterator <String> iter = new Iterator <> () {
    @Override
    public boolean hasNext() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public String next() {
        // TODO Auto-generated method stub
        return null;
    }

};


The above snippet will throw the compile time error "<> cannot be used with anonymous classes." But the same code will be successfully compiled in Java 9.

Java (programming language) COinS

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • NEXT.JS 13: Be Dynamic Without Limits
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • What Is Policy-as-Code? An Introduction to Open Policy Agent

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: