09/13/12All Rights Reserved - ADVANTEST CORPORATION1 Java 7 Highlights Presented By: Andrey Loskutov.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Yoshi
1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
Written by: Dr. JJ Shepherd
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Some basic I/O.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java Writing and reading objects to and from.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Introduction to Java.
Reading/Writing Files, Webpages CS2110, Recitation 10 1.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Performance measurements for inter-process communication.
Java Exception Handling Handling errors using Java’s exception handling mechanism.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Java and C++, The Difference An introduction Unit - 00.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
20-Oct-15 Java 7. The diamond operator Instead of HashMap > map = new HashMap >(); you can now say HashMap > map = new HashMap; In fact, if you don’t.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
1 Web Based Programming Section 8 James King 12 August 2003.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
File IO Basics By Dan Fleck Coming up: Data Streams.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Java Thread and Memory Model
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Written by: Dr. JJ Shepherd
All Rights Reserved - ADVANTEST CORPORATION1 Java 7 Highlights Presented By: Andrey Loskutov.
All Rights Reserved - ADVANTEST CORPORATION1 Java 7 Highlights Presented By: Andrey Loskutov.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
OO Design and Programming II I/O: Reading and Writing
JAVA MULTIPLE CHOICE QUESTION.
Strings and File I/O.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Creating and Modifying Text part 2
Exceptions 10-Nov-18.
Introduction to Programming in Java
Web Design & Development Lecture 7
Units with – James tedder
Exceptions 10-May-19.
EEC 484/584 Computer Networks
Presentation transcript:

09/13/12All Rights Reserved - ADVANTEST CORPORATION1 Java 7 Highlights Presented By: Andrey Loskutov

09/13/12All Rights Reserved - ADVANTEST CORPORATION2 Agenda Motivation API Changes Highlights IO and New IO Java Programming Language Changes Highlights Binary Literals Strings in switch Statements The try-with-resources Statement Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking Underscores in Numeric Literals Type Inference for Generic Instance Creation Known issues

09/13/12All Rights Reserved - ADVANTEST CORPORATION3 Motivation Java release 7 represents (from Java 6 point of view): new platform vendor (Oracle) 5 years work on the next major Java release all fixes and features of ~20 minor Java 6 releases new G1 garbage collector compressed 64-bit pointers better support for scripting languages ("invokedynamic") Java bytecode specification changes Java language syntax changes lot of new API's various other smaller enhancements… *Note for C/C++ programmers: … code written for Java 1.1 (1997) is still supported on 1.7 (2012)

09/13/12All Rights Reserved - ADVANTEST CORPORATION4 Agenda Motivation API Changes Highlights IO and New IO Java Programming Language Changes Highlights Binary Literals Strings in switch Statements The try-with-resources Statement Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking Underscores in Numeric Literals Type Inference for Generic Instance Creation Known issues

09/13/12All Rights Reserved - ADVANTEST CORPORATION5 API Changes: IO and new IO New convenient API for dealing with paths and files: java.nio.Path (inspired by org.eclipse.core.runtime.IPath) java.nio.Paths (creates Path's from strings and URL's) java.nio.Files (all things you missed in java.io.File) You can now have full access to symlinks manage all file attributes via FileAttribute open directories as DirectoryStream walk file trees as FileVisitor copy files to streams (Files.copy(file, System.out)) watch file system for changes (WatchService) create virtual file systems (FileSystemProvider) query file content type (FileTypeDetector)

09/13/12All Rights Reserved - ADVANTEST CORPORATION6 IO and new IO examples TBD

09/13/12All Rights Reserved - ADVANTEST CORPORATION7 Agenda Motivation API Changes Highlights IO and New IO Java Programming Language Changes Highlights Binary Literals Strings in switch Statements The try-with-resources Statement Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking Underscores in Numeric Literals Type Inference for Generic Instance Creation Known issues

09/13/12All Rights Reserved - ADVANTEST CORPORATION8 Binary Literals You can now express integral type numbers in binary number system by using only 0 and 1. All what you need is to start the value with 0b or 0B: The reason is usability: it is much easier to use binary numbers while manipulating bits. For example, you can easily see that each number in the series above is rotated by one bit. Now try to see this with decimals: 73, 36, 146, 73 byte b = 0b ; short s = 0B ; int i = 0b ; long l = 0B ; byte b = 0b ; short s = 0B ; int i = 0b ; long l = 0B ;

09/13/12All Rights Reserved - ADVANTEST CORPORATION9 Strings in switch Statements You can now use a String object in the expression of a switch statement. For string matching String.equals() is used, so switch on strings is case sensitive null strings will cause NullPointerException (surprise!) public static void main(String[] args) { String os = System.getProperty("os.name"); switch (os) { case "Linux": System.out.println("Cool!"); break; case "Windows": System.out.println("Not so cool!"); break; default: System.out.println("Obst?"); break; } public static void main(String[] args) { String os = System.getProperty("os.name"); switch (os) { case "Linux": System.out.println("Cool!"); break; case "Windows": System.out.println("Not so cool!"); break; default: System.out.println("Obst?"); break; }

09/13/12All Rights Reserved - ADVANTEST CORPORATION10 try-with-resources Let's remember how we write to files in Java 6: 15 lines of code (and 19 lines to read this file back)! This is mostly due the standard try/finally pattern for properly closing resources in Java 6. FileWriter fw = null; try { fw = new FileWriter(file); fw.write("Hello Java 6!"); } catch (IOException e) { e.printStackTrace(); } finally { if(fw != null){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); } FileWriter fw = null; try { fw = new FileWriter(file); fw.write("Hello Java 6!"); } catch (IOException e) { e.printStackTrace(); } finally { if(fw != null){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); }

09/13/12All Rights Reserved - ADVANTEST CORPORATION11 try-with-resources (continued) Welcome to the Java 7 world: write to file (5 lines instead of 15): read from file (8 lines instead of 19): same as above in 1 line (using new Files API): Cool, isn't? try (FileWriter fw = new FileWriter(file)) { fw.write("Hello Java 7!"); } catch (IOException e) { e.printStackTrace(); } try (FileWriter fw = new FileWriter(file)) { fw.write("Hello Java 7!"); } catch (IOException e) { e.printStackTrace(); } try (BufferedReader br = new BufferedReader(new FileReader(file))){ String line; while((line = br.readLine()) != null){ System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } try (BufferedReader br = new BufferedReader(new FileReader(file))){ String line; while((line = br.readLine()) != null){ System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } Files.copy(file.toPath(), System.err);

09/13/12All Rights Reserved - ADVANTEST CORPORATION12 try-with-resources (how it works) In try() statement one can declare one or more resources. Try-with-resources ensures that each resource is closed at the end of the try{} statement. A resource must implement java.lang.AutoCloseable java.io.Closeable interface extends AutoCloseable since 1.7, e.g. most streams can be used now in try() statement Any catch or finally block is run after the resources declared have been closed Note: in case multiple resources are declared in try(), they are closed in the opposite definition order: In example above "to" stream is closed first, "from" second. try ( FileReader from = new FileReader(f1); FileWriter to = new FileWriter(f2)) { int data; while ((data = from.read()) != -1) { to.write(data); to.write(System.lineSeparator()); } } catch (IOException e) { e.printStackTrace(); } try ( FileReader from = new FileReader(f1); FileWriter to = new FileWriter(f2)) { int data; while ((data = from.read()) != -1) { to.write(data); to.write(System.lineSeparator()); } } catch (IOException e) { e.printStackTrace(); }

09/13/12All Rights Reserved - ADVANTEST CORPORATION13 Known problems Yourkit has bug if profiling such code (with 1.7 target): Solution 1: wait for the Yourkit bugfix (bug reported). Solution 2: add "stripstackmaptable" option to the YourKit launch configuration If urgently needed, simply change the code to: public class Main { public static void main(String[] args) { boolean b = Math.random() > 0.5; new String(b == true? "hallo" : "bug"); } [YourKit Java Profiler ] Exception in thread "main" java.lang.VerifyError: Inconsistent stackmap frames at branch target 46 in method Main.main([Ljava/lang/String;)V at offset 38 public class Main { public static void main(String[] args) { boolean b = Math.random() > 0.5; new String(b == true? "hallo" : "bug"); } [YourKit Java Profiler ] Exception in thread "main" java.lang.VerifyError: Inconsistent stackmap frames at branch target 46 in method Main.main([Ljava/lang/String;)V at offset 38 public class Main { public static void main(String[] args) { boolean b = Math.random() > 0.5; String s = b == true? "hallo" : "bug"; new String(s); } public class Main { public static void main(String[] args) { boolean b = Math.random() > 0.5; String s = b == true? "hallo" : "bug"; new String(s); }

09/13/12All Rights Reserved - ADVANTEST CORPORATION14 Links Official Java 7 Documentation html Tutorial new IO Java History All examples and this presentation