12:02 am - Saturday June 24, 2017

jdk 7 Features

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

1)Sample.java

Before Java 7:

int datatype allows only numbers.

In Java 7:

  int allows numbers and underscore.

So, it is easy to read the numbers. Especially when declare amount, in integer it’s easy to read it and avoid confusion.

import java.io.*;

class Sample

{

public static void main(String args[])throws IOException

{

int i;

//Java 7 allows underscore in integer

i=6786_79_8;

System.out.println(i);

}

}

2) SampleArrayList

Before Java 7:

List<String> al=new ArrayList<String>();

In Java 7:

List<String> al=new ArrayList<>();

It doesn’t need to specify the type of ArrayList at the end.

//SampleArrayList.java

import java.util.*;

class SampleArrayList

{

public static void main(String args[])

{

List<String> al=new ArrayList<>();

al.add("first");

al.add("second");

System.out.println(al);

}

}

Output:

[first, second]

3) SampleSwitch.java

Before Java 7:

String not allowed in switch statement. Switch allows only character and numeric. If we need to compare a string in switch statement, assign string values to an numeric and use it in switch statement.

In Java 7:

Java 7 allows String in switch statement. So, we can directly use strings in switch statement.


//SampleSwitch.java

class SampleSwitch

{

public static void main(String args[])

{

String s="java";

//switch allows string from java7

switch(s)

{

case "java":

System.out.println("This is java");

break;

case "j2ee":

System.out.println("This is j2ee");

break;

}

}

}

Output:

This is java

4) SampleException.java

Before Java 7:

catch(ArithmeticException e){}
                   catch(NumberFormatException){}

                   catch(NullPointerException){}

                   catch(IOException){}

In Java 7: catch(ArithmeticException|NumberFormatException|NullPointerException|IOException e){}

It’s more simple than the previous. Multiple catch can be handled in one catch statement.


//SampleException.java

import java.io.*;

class SampleException

{

public static void main(String args[])

{

try

{

DataInputStream d=new DataInputStream(System.in);

int a,b,c;

System.out.println("Enter First No.");

a=Integer.parseInt(d.readLine());

System.out.println("Enter Second No.");

b=Integer.parseInt(d.readLine());

c=a/b;

System.out.println("a/b= "+c);

}

//Multiple exception catch in single catch statement using "|" symbol.

catch(ArithmeticException|NumberFormatException|NullPointerException|IOException e)

{

System.out.println("this is exception"+e);

}

}

} 

Output:

Enter First No.

4

Enter Second No.

this is exceptionjava.lang.NumberFormatException: For input string: “”

5) TryWithResource.java

Before Java 7:

Resources are need to be closed manually. After processing.

In Java 7:

It implements autoclosable method.

Close method run automatically.

Need not to close the resources manually.

TryWithResource.java


public class TryWithResource {

public static void main(String[] args) {

try(Resource res = new Resource("Resource closing")){

res.put("Listen to Put");

} catch(Exception e){

System.out.println("Exception: "+

e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());

}

}

}

Resource.java


class Resource implements AutoCloseable{

String closingMessage;

public Resource(String closingMessage) {

this.closingMessage = closingMessage;

}

public void put(String work) throws ExceptionA{

System.out.println(work);

throw new ExceptionA("ExceptionA");

}

public void close() throws ExceptionB{

System.out.println(closingMessage);

throw new ExceptionB("ExceptionB");

}

public void put(Resource res) throws ExceptionA{

res.put("Put method runs");

}

}

ExceptionA.java


public class ExceptionA extends Exception

{

public ExceptionA(String a)

{

super(a);

}

}



ExceptionB.java


public class ExceptionB extends Exception

{

public ExceptionB(String a)

{

super(a);

}

}



Output:

Listen to Put

Resource closing

Exception: ExceptionA Thrown by: ExceptionA

Filed in: Jdk 7 Released

Comments are closed.