There are several new features that have been released in Java 7 to take care of several developers’ requirements. However, interestingly, I have figured out that three of those 7 features are primarily aimed to enhance following key characteristics of code quality. Please feel free to share your opinion on them.
- Code Readability
- Code Duplication
- Code Complexity
Following is the detail of those 3 Java 7 features:
- Underscore in Numeric Literals (Enhances Code Readability)
This feature primarily takes care of code readability, and hence, code usability, having large numbers having several digits. For example, credit card number, social security number etc. Many a times, we come across large number which seem to have an embedded pattern and when written as it is becomes difficult to read. This is when this feature comes handy. For example, credit card number. It is a 16 digit number which could be split into group of 4. Thus, the number assignment would look like following:long creditCardNumber = 4352_2314_8710_1210;
long socialSecurityNumber = 8888
You could read several such examples for long, float, byte variables in the Java doc page.
- Catching Multiple Exception Types (Reduces Code Duplication)This feature primarily helps in reducing code duplication in relation with multiple catch blocks to handle more than one exception type. With this feature, a single catch can handle more than one type of exception. Take a look at following code samples:Prior to Java 7 release, one would be required to write in following manner to catch multiple exceptions. Note the code duplication aspect.
Post Java 7 release, one needs to separate different exceptions with | such as following. Note the simplified code thereby reducing code duplication.
- String in Switch (Reduces Code Complexity)
This one is primarily aimed to reduce code complexity associated with usage of if-else for comparing string or mapping those String to final integer constant or char constant to use them inside switch. You may want to check StringSwitchDemo from JavaDocs to make yourself familiar with the code. However, it looks like same old Java switch statement with change that case statements could not have string literals which it could not have prior Java 7 release. Take a look at sample below:
- Generative Modeling in Machine Learning: Examples - March 19, 2023
- Data Analytics Training Program (Beginners) - March 18, 2023
- Histogram Plots using Matplotlib & Pandas: Python - March 18, 2023
How about try with resource?