List of Major Java 7 Features – How java programming language has changed in JDK 7

java_logo
Ever wondered what things change in java SE from Java 6 to Java 7 version? Complete list is located here but I am going to focus on the language change. I have provided examples to make it more clear wherever applicable.

  1. The integral types byte, short, int and long can also be expressed as binary number by adding prefix 0b. Some examples below.
    int five = 0b101;
    long someLong = 0b1010000101000101101000010100010111100001010001011010000101000101L;
    
  2. Underscores in Numeric Literals for readability.
    long million = 1_000_000_000L;
    double length = 123_234.045_456
    
  3. You can now use String in switch statement
  4. Improvement to Generics – No longer need to specify type in both left and right side. Diamond still required to avoid warning.
    e.g. Map<String, List> myMap = new HashMap<>();
  5. Try-with-resources Statement
    try (BufferedReader br = new BufferedReader(new FileReader(path))) {
    return br.readLine();
    }
    

    In the example above br will be closed regardless of whether the statements within the try block executed normally or got exception. There can be multiple resources defined within the try parenthesis. If there are multiple resources, the last resource is closed first, the first one is closed last.

    try (
    BufferedReader reader = new BufferedReader(new FileReader(path));
    BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
    ) {
    }
    

    In this example, writer gets closed first, then the reader gets closed.

  6. Multiple Exception Handling
    catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
    }
    
  7. Rethrowing Exceptions with Improved Type Checking
    public void rethrowException(String exceptionName) throws FirstException, SecondException {
    try {
    // ...
    }
    catch (Exception e) {
    throw e;
    }
    }
    

    Only FirstException and SecondException are thrown (even if they are declared to be children of Exception). All other exceptions are caught.

Date From Timestamp in Long format

Generally, the dates are stored in long timestamp format in the database.
This simple class can convert the timestamp date to real date format.

package com.kushal.util;
import java.util.Date;
public class DateFromLong {

	public static void main(String ags [])
	{
		long dateInLongFormat=1212499200000L;
		Date date=new Date(dateInLongFormat);

		System.out.println(date);

	}

}

Here is the sample output for the long date give above:

Tue Jun 03 08:20:00 CDT 2008

How to resolve – java.lang.VerifyError java.lang.SecurityException: Signers of org.junit.runner.Runner do not match signers of other classes in package

Problem:

Have you ever encountered the following exception when you were trying to run a JUnit Class with isolation framework like JMockit?

java.lang.VerifyError: java.lang.SecurityException: Signers of 'org.junit.runner.Runner' do not match signers of other classes in package
	at java.lang.J9VMInternals.verify(J9VMInternals.java:74)
	at java.lang.J9VMInternals.initialize(J9VMInternals.java:129)
	at org.junit.runner.Request.aClass(Request.java:45)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.(JUnit4TestClassReference.java:24)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Root Cause:

After quite a few attempts and reserch, I discovered that the error is due to the conflict with the default version of the JUnit provided by the eclipse.

Solution:

Follow the following steps to resolve the issue. Happy JUniting and JMockiting.

  1. Remove the build reference to existing JUnit jar file from your build path.
  2. Download and use the independent Junit Jar file in your library.