Previous | Home | Next |
Sample.java
Before Jdk1.7
int datatype allows only numbers.
In Java1.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=2386_49_5; System.out.println(i); } }
Yes
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> a=new ArrayList<>(); a.add("one"); a.add("two"); System.out.println(a); } }
Output:
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:
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:
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
- Java 1.6 runs faster than Java 1.5.
- Java 1.6 makes programming easier by implementing various tools such as SwingWorker and JTable to develop user interface.
- In java 1.6,Java DB, a new database management tool, has been included. Java DB is based on the open-source Apache Derby and is supported by Sun.
- Jdk 6 provides high performance, reliability, and UI improvements.
- Jdk 6 is having expanded monitoring and diagnositics capacities which delivers dramatic out-of-the-box benefits without any coding changes or even a re-compile necessary.
- Java SE 6 streamlines web service and XML development, simplifies GUI development and augments native desktop support, expands programmatic access to native security facilities, and is the first release to offer a standardized framework for scripting languages.
Previous | Home | Next |