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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube

Trending

  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • Building a Zero-Cost Approval Workflow With AWS Lambda Durable Functions
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  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.

By 
Gowtham Girithar Srirangasamy user avatar
Gowtham Girithar Srirangasamy
·
Jul. 21, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
12.1K 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.

Related

  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook