6.5 million software developers and still going strong

Java Developer Magazine

Subscribe to Java Developer Magazine: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Java Developer Magazine: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Java Developer Authors: APM Blog, Stackify Blog, Pat Romanski, Hollis Tibbetts, Andreas Grabner

Related Topics: Java EE Journal, JavaBlackBelt, Innovations Software Technology, Java Developer Magazine, Java in the Cloud

Blog Post

Revisiting Java SE 7 Features | @CloudExpo #Java #Cloud #OpenSource

A tutorial series on Java as we all eagerly await the official release of Java SE 9

Preparing for an interview? Want to just revisit Java SE 7 features? Trying to recollect or revise a Java SE programming construct? Let me take you back in time to what was introduced first in Java SE 7? Join me for this tutorial series on Java as we all eagerly await the official release of Java SE 9.

Java SE 7 Release Date: 28-07-2011

Java SE 7 Code Name: Dolphin

Java SE 7 Highlights

  • Strings in switch statements.
  • Automatic resource management in try statements.
  • Improved type inference for generic instance creation, AKA the diamond operator <>.
  • Simplified varargs method declaration.
  • Binary integer literals.
  • Allowing underscores in numeric literals.
  • Catching multiple exception types and rethrowing exceptions with improved type checking.

I have provided some of the most important core language enhancements for JDK 7.0, along with code samples. The examples provided below can be directly pasted into your IDE, and you may name the class as provided.

Strings in Switch Statements
You may use Strings in your switch statements, which is a powerful tool. The alternative would have been to use a series of if statements with an .equals() comparison. You may directly execute the code below in Eclipse to understand it better.

public class jdk7_StringSwitch {
static String day = "SAT";
public static void main(String[] args) {
switch(day) {
case "MON": { System.out.println("Monday"); break; }
case "TUE": { System.out.println("Tuesday"); break; }
case "SUN": { System.out.println("Sunday"); break; }
case "SAT": { System.out.println("Saturday"); break; }
}
}
}

Multiple Exception Handling
Before JDK7, you would have to catch multiple exceptions in a block - with a catch block for each of the thrown checked exceptions. But sometimes you may want to take actions for many exceptions that are similar or the same. Hence, the utility of this feature. You may directly execute the code below in Eclipse to understand it better.

public class jdk7_ExceptionHandling {
public static void main(String[] args) {
try {
Integer value = Integer.parseInt("ABC");
File file = new File("exception.txt");
FileReader fileReader = new FileReader(file);
} catch(NumberFormatException | FileNotFoundException foe) {
System.out.println("Multiple Exception Types");
}
}
}

Try With Resources
You may use/create a resource withing the try block, from JDK7, and use it within the following block. This includes all objects/resources that implement java.io.Closeable. Automatically, on encountering the exception or if there is no exception, the close() method is guaranteed to be called by the runtime.

public class jdk7_TryWithResources {
public static void main(String[] args) throws IOException {
try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
br.readLine();
} finally {
System.out.println("All Resources Closed.");
} }
}

Make sure that you have a file.txt in your classpath.

More Stories By Sumith Kumar Puri

Sumith Kumar Puri, an author (Java/JEE) at Packt Publishing, has close to 14 years of experience in entrepreneurship, conceptualization, architecture, design and development of software products and solutions. He also holds multiple certifications in Core Java, JEE, C, C++, Algorithms and Data Structures. He is an open source contributor and likes competitive programming and hackathons. He has 11 publications to his name in international and national conferences, journals and magazines. He is an active member of the IEEE/Computer Society, ACM and CSI.

He holds a Bachelor of Engineering [Information Science and Engineering] from Sri Revana Siddeshwara Institute of Technology, Bangalore, India. He has also completed the Executive Programme [Data Mining and Analytics] from the Indian Institute of Technology, Roorkee, India and the Executive Certificate Programme [Entrepreneurship] from the Indian Institute of Management, Kashipur, India. His current interests are in data mining, machine learning, Big Data and artificial intelligence. He is based out of Bangalore, India.