Recent Tutorials and Articles
    Java 7 New Features
    Published on: 2018-05-25 16:05:30

    This tutorial covers the new features added in java programming language as part of Java7 release.

    Please note that this tutorial doesn't contain the full list of new features introduced in Java SE7. This rather concentrates on changes to Java Programming Language and doesn't go in details of changes to I/O, concurrent utilities, Security, JVM, Networking, RIA, JDBC changes. For release notes of java7, please visit here.

    Java Programming Language

    Binary Literals:

    Starting Java7, all the number types (byte, short, int, long) can be expressed using the binary number system. In order to do same, add the prefix 0b or 0B to the number as shown in following examples. This could be helpful in getting better visibility when doing the bit level operations -

    //byte value (8-bit) expressed in binary number system
    byte numByte = (byte) 0b10010001;
    
    //short value (16-bit) expressed in binary number system
    short numShort = (short) 0b1001000100011110;
    
    //int value (32-bit) expressed in binary number system
    int numInt =  0b10010001000111101010101001010101;
    
    //long value (64-bit) expressed in binary number system
    long numLong =  0b1001000100011110101010100101010110010001000111101010101001010101L;
    
    

    Strings in Switch Statements:

    Finally you can use a String object in a switch statement expression. The switch statement compares the expression String with each case lable by calling String.equals method. Here is an example of that -

     

    Java7StringsInSwitchStatement.java
    package com.sts.allprogtutorials;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class Java7StringsInSwitchStatement {
    
        /**
         * @param args
    	 */
    	public static void main(String[] args) {
    		final String programmingLanguage = "Java";
    		
    		String programmingLanguageType = null;
    		switch(programmingLanguage) {
    		case "C++"  :
    		case "Java" : 
    			programmingLanguageType = "Object Oriented Programming Language";
    			break;
    		case "C"	:
    			programmingLanguageType = "Procedural Programming Language";
    		}
    		
    		System.out.println("Programming Language: " + programmingLanguage + " is of type: " + programmingLanguageType);
    	}	
    }
    
    

    If you run the above program, you get output as 'Programming Language: Java is of type: Object Oriented Programming Language'.

    The try-with-resources Statement:

    Have you ever missed to close any stream/jdbc statements or you are annoyed with closing the streams to close everytime you use it? Well, there is a good news for you that developers no longer need to close the streams/statements explicitly as it is taken care by JVM automatically if you use try-with-resources.

    This is how a typical code to read the data from a table using JDBC will look like before java7 -

    public static void printEmployeesWithoutTryWithResource(Connection connection) {
    		
    	PreparedStatement preparedStatement = null;
    	ResultSet resultSet = null;
    	
    	String query = "select EMP_ID, EMP_NAME, EMP_EMAIL, EMP_DOB from EMPLOYEE";
    	try{
    		preparedStatement = connection.prepareStatement(query);
    		resultSet = preparedStatement.executeQuery();
    		
    		while(resultSet.next()) {
    			final Long empId = resultSet.getLong("EMP_ID");
    			final String empName = resultSet.getString("EMP_NAME");
    			final String empEmail = resultSet.getString("EMP_EMAIL");
    			final Date dOB = resultSet.getDate("EMP_DOB");
    			System.out.println("[" + empId + ", " + empName + ", " + empEmail + ", " + dOB + "]");
    		}
    	} catch (SQLException sqlException) {
    		sqlException.printStackTrace();
    	} finally {
    		try {
    			if(resultSet != null && !resultSet.isClosed()) {
    				resultSet.close();
    			}
    	
    			if(preparedStatement != null && !preparedStatement.isClosed()) {
    				preparedStatement.close();
    			}
    		}catch (SQLException sqlException1) {
    			sqlException1.printStackTrace();
    		}
    	}
    }
    
    

    Here is how it will look like if you use try-with-resources statement (notice parenthesis after try) -

    public static void printEmployeesUsingTryWithResource(Connection connection) {
    	
    	String query = "select EMP_ID, EMP_NAME, EMP_EMAIL, EMP_DOB from EMPLOYEE";
    	try(
    		PreparedStatement preparedStatement = connection.prepareStatement(query);
    		ResultSet resultSet = preparedStatement.executeQuery();
    	){
    		while(resultSet.next()) {
    			final Long empId = resultSet.getLong("EMP_ID");
    			final String empName = resultSet.getString("EMP_NAME");
    			final String empEmail = resultSet.getString("EMP_EMAIL");
    			final Date dOB = resultSet.getDate("EMP_DOB");
    			System.out.println("[" + empId + ", " + empName + ", " + empEmail + ", " + dOB + "]");
    		}
    	} catch (SQLException sqlException) {
    		sqlException.printStackTrace();
    	} 
    }
    
    

    Please note that PreparedStatement and ResultSet used above belong to JDBC 4.1 and extend AutoCloseable interface. In general, any class/interface implementing/extending AutoCloseable or Closeable (it extends AutoCloseable) interface, can be initialised (read autoclosed) in try-with-resources statement.

    Above example doesn't show finally block but you can use finally just like you use it in traditional try block.

    Handling More Than One Type of Exception in One Catch Block:

    Java7 lets you handle more than one type of exceptions in one catch block and therefore reducing redundancy in the code. Let's have a look at following code handing 3 different type of exceptions prior to java7 -

    try {
    	//Statements that could throw either SQLEXception or IOException
    } catch(SQLException exception) {
    	exception.printStackTrace();
    	throw exception;
    } catch(IOException exception) {
    	exception.printStackTrace();
    	throw exception;
    }
    
    

    And here is how it will look like in java7 -

    try {
    	//Statements that could throw either SQLEXception or IOException
    } catch(SQLException|IOException exception) {
    	exception.printStackTrace();
    	throw exception;
    }
    
    

    You can handle as many exceptions as you want in this way but one thing worth mentioning is that all of the exceptions must be siblings i.e. if you can not use any of the exception which is parent/child of any of other exceptions already specified in catch block. For instance, following will not work -

    try {
    	//Statements that could throw either SQLEXception or IOException
    } catch(SQLException|Exception exception) { //Compilation error as Exception is parent of SQLException
    	exception.printStackTrace();
    	throw exception;
    }
    
    

    Underscores in Numeric Literals:

    Starting Java 7, numerical literals can have any number of underscore characters anywhere between the digits. But no underscores are allowed in starting or ending of literal. This can be useful when you are dealing with big numbers or non-decimal number system and wants to make it more readable. Following are some of the valid and non-valid expressions -

    Valid Numeric Expressions containing Underscore characters
    long hex = 0xAE_FF_FE_DE;
    long creditCardNo = 4321_8765_1234_5678L;
    float pi = 1_23.45_67_89F;
    double amount = 1_257_434_656.34;
    
    
    Non-valid Numeric Expressions containing Underscore characters
    long creditCardNo1 = _4321_8765_1234_5678L; //can't put underscore in starting of number as it makes it an identifier
    long creditCardNo2 = 4321_8765_1234_5678L_; //can't put underscore in the end
    long creditCardNo3 = 4321_8765_1234_5678_L; //can't put underscore before 'L' as it is same as putting underscore in the end
    long hex = 0x_AE_FF_FE_DE; //can't put underscore after 0x
    float pi = 1_23._45_67_89F; //can't put underscore just after decimal point(.) 
    double amount = 1_257_434_656_.34; //can't put underscore just before decimal point(.)
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 2018-05-25 16:05:30

    Comment Form is loading comments...