0
Sponsored Links


Ad by Google
Still popular java interview question, yesterday I was appeared in one of the world's largest investment banking company's interview and interviewer asked me to list down the new features of java 7
I thought it was java 8 but when, I re-confirm then interviewer replied yes you have to list down the new features of java7.

So, I decided to post a topic on new features of java 7, In this post I am going to list down all the new features of java7 with code example.
The java 7 features are still on demand not only in real time use but in interview also like java program to balanced parentheses

Below are the list of new features of java 7
  1. String in switch
  2. Try-with-resources statement
  3. Underscores in numeric literals
  4. Binary literals
  5. Multiple exception catching
  6. Improved Type Inference for Generic Instance Creation
  7. SafeVarargs
String in switch: Now you can use Strings in switch statements.
public static void stringInSwitch(String months) {

		final String JAN = "january";
		final String FEB = "february";
		final String DEC = "December";

		switch (months) {
		case JAN:
			System.out.println("Repulic Day");
			break;
		case FEB:
			System.out.println("Valentine's Day");
			break;
		case DEC:
			System.out.println("Christmas");
			break;
		}
	}

Try-with-resources statement:
public static void tryWithResources() throws IOException {
		try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
			System.out.println(br.readLine());
		}

	}

Underscores in numeric literals:
public static void _InNumeric() {
		int one_million = 1_000_000;
		System.out.println(one_million);

	}

Binary literals:
public static void binaryLiterals() {
		int binary = 0b1001_1001;
		System.out.println(binary);
	}

Multiple exception catching:
public static void multipleException() {
		try {
			BufferedReader br = new BufferedReader(new FileReader("test.tx"));
			Object x = new Integer(0);
		} catch (FileNotFoundException | ClassCastException e) {
			e.printStackTrace();
		}
	}

Improved Type Inference for Generic Instance Creation:
Map<String, List<String>> myMap = new HashMap<>();

SafeVarargs:

        @SafeVarargs
	public static void safeVarargs(List<String>... lists) {
		for (List<String> list : lists) {
			System.out.println(list);
		}
	}

Sponsored Links

0 comments:

Post a Comment