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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Trending

  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • Optimize Deployment Pipelines for Speed, Security and Seamless Automation
  • Rethinking Recruitment: A Journey Through Hiring Practices
  • AI’s Role in Everyday Development
  1. DZone
  2. Coding
  3. Java
  4. Java 9 (Part 4): Trying Try-with-Resources: First Look

Java 9 (Part 4): Trying Try-with-Resources: First Look

In Part 4 of this Java 9 series, we see that try-with-resources has been enhanced. Let's compare how Try With Resources worked back in Java 7 versus today.

By 
Tomer Ben David user avatar
Tomer Ben David
·
Updated May. 27, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
21.5K Views

Join the DZone community and get the full member experience.

Join For Free

This is part 4 of the java 9 series. While in part 3 we covered interfaces and the private methods that were added to them, in part 4 we see that try-with-resoures was enhanced, or we might say even fixed. Try-with-resources, which was introduced back in java 7, is a concept which helps developer close resources which are not using anymore, such as database connection, file streams, etc. Let’s see how try-with-resources behaved in java 7 and now in java 9.

Image title

Step 1: Pre-Java 7 Try-with-Resources

Try-with-Resources, prior to Java 9, meant that instead of just calling a piece of code which opens up a resource like:

InputStream input = null;
try {
    // open some resource note we are inside the try block.
    input = new FileInputStream("somefile");
} finally {
    // Hey don't forget to close them! we are not java 7 yet!
    if (input != null)
        input.close();
}

As you see in the above example we have used a standard try-and-finally.

Step 2: Try-with-Resources From Java 7 and On 

In Java 7, we can put the opening of the stream itself inside the try() block note that now try has brackets - try()

So we use it as:

try(FileInputStream input = new FileInputStream("somefile")) {

}


Where is the close() you ask? Java will take care of that. After all, it’s try with resources.

Step 3: Try-with-Resources From Java 9 and On

If we have multiple resources and we are using them and we want try-with-resources to close them all, now with Java 9, it's possible even without introducing a new variable- if our resource is already with the final keyword, that is.

final Resource resource1 = new Resource("resource1");
Resource resource2 = new Resource("resource2");

try(resource1; resource2 ...) { // couldn't do that before java 9
  // do something with the resources.
}

However, with Java 9 we can:

final Resource resource1 = new Resource("resource1");
Resource resource2 = new Resource("resource2");

try(resources1, resources2) { // possible with java 9
}

In Part 1 of this series we covered Java 9 modules, in Part 2, JShell, and then we went on to super interfaces in Part 3. In this Part 4, we saw that try-with-resources was greatly enhanced to allow us to refer to multiple seamlessly without the need to define any intermediate variable. So, you just try-with-resources seamlessly for multiple resources!

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!