User:Mwn3d/Java 7 stuff: Difference between revisions

From Rosetta Code
Content added Content deleted
(Some Java 7 notes...not sure where this explanation can fit in yet)
 
m (More accurate use for divide and conquer methods)
 
Line 12: Line 12:
Infers types to place inside <code><></code>. Uses the most specific types that it can based on the types on the left (<code>String</code> and <code>Number</code> for this example), rather than just copying text from the left to the right.
Infers types to place inside <code><></code>. Uses the most specific types that it can based on the types on the left (<code>String</code> and <code>Number</code> for this example), rather than just copying text from the left to the right.


''Divide and conquer methods for multiple CPUs coming once I understand it''
''Divide and conquer methods for multicore CPUs coming once I understand it''

Latest revision as of 02:40, 31 July 2011

Try with resources: <lang java5>try(BufferedWriter output = new BufferedWriter(new FileWriter("fileName.txt" , true));

               BufferedReader input = new BufferedReader(new FileReader("otherFile.txt"))/*;other streams if you want*/){
   output.write("some text" + input.readLine());

}catch(IOException e){

   System.err.println("I'm sorry, Dave, but I can't let you do that");

}</lang> The readers and writers are closed automatically. Exceptions can be caught for initialization errors, writing/reading errors, and closing errors. This also checks for nulls when closing, so it removes the need for nested try-catch-finally blocks when using multiple readers/writers.

Diamond type inference: <lang java5>Map <String, List<? extends Number>> numbers = new HashMap<>();</lang> Infers types to place inside <>. Uses the most specific types that it can based on the types on the left (String and Number for this example), rather than just copying text from the left to the right.

Divide and conquer methods for multicore CPUs coming once I understand it