WHAT WERE MISSING IN JAVA 7 BECO stance

WHAT WE’RE MISSING IN JAVA 7

BE/CO stance � No move to Java 7 until end of this year � IT deploy Java 7 everywhere BE/CO remove it again! So don’t rely on it being installed on a console � ‘free’ support for Java 1. 6 stops this summer Have to pay afterwards � � We can’t specify 1. 7 in our JNLP files a Ja. WS would need internet access to download 1. 7 JRE Many other new features, but I’ll outline what was interesting for me…

Switches on Strings � � � Just like primitive switch() switch (day. Of. Week. Arg) { case "Monday": … Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-thenelse statements

Underscores in numeric literals � int toto 4 = 5_______2; � Why? � Readability! Not everywhere though � 3_. 1415 <- Not next to a decimal point � 999_99_9999_L <- Not next to F or L suffix � 52_ <- Not at the end of a literal � 0_x 45 <- Can’t separate 0 x! � Rule is that if it doesn’t make it more readable, it’s invalid!

Binary literals � � Now we can have literals expressed in binary 0 b or 0 B prefix (like 0 x/0 X for hex) � 0 b 00110001 � � This can make bit patterns much easier to understand than using hexadecimal for example Same rules for 64 bits apply as decimal � 0 b 10100001010001011010000101 L

Type inference for Generics � Typing reduced � Map<String, List<String>> my. Map = new Hash. Map<String, List<String>>(); � …becomes… � Map<String, List<String>> my. Map = new Hash. Map<>(); Compiler knows what it’s expecting so infers it automatically!

Catching Multiple Exception Types � � Often several exceptions are thrown from a single call to a method Java 7 allows you to ‘OR’ the exceptions in the catch statement � try{…}catch � (IOException|SQLException ex) { } Also, you can promote a class to a subclass in the throws declaration � Void toto(String s) throws AEx, BEx{ try { //. . . } catch (Exception e) { throw e; } } � The compiler checks that e is indeed of type Aex or Bex � This is really useful when catching many exceptions (generically) and rethrowing them with more inclusive type checking

� � You can now ‘monitor’ files or directories Can be used as a handy inter-processcommunication mechanism � Used in FESA 3 Navigator (using 3 rd party library at the moment) � ‘notify me when a new file appears in a directory’ � Uses native system calls (Read. Directory. Changes. W / i. Notify on Windows / Linux) On other platforms falls back to polling

File info � � Very poor info prior to Java 7 Metadata (size, owner, creation time, etc) � Also is. Symbolic. Link(Path) � ? et. Posix. File. Permissions(Path, Link. Option. . . ) � … + a lot more attributes depending on file system (windows, posix etc) � Can now give much better error messages when something goes wrong with a file access � Previously you just got ‘false’ when trying to do many file operations! � File systems info as well

Easier support for collections � Previously it was very cumbersome to intialise Lists/Sets/Maps etc. � Inialising Lists List<String> list = ["item"]; � Accessing Lists String item = list[0]; � Initializing Sets Set<String> set = {"item"}; � Maps… Map<String, Integer> map = {"key" : 1}; int value = map["key"]; � Etc… � Result is much less code and typing!

Conclusion � � We should take care not to use any new features in Java 7 if we intend to use in CCC We _could_ use from our terminal servers Main benefits are for IO (Sockets, NIO. 2 not in detail here) But lots of nice language modifications � Less � typing! We now wait for CO!
- Slides: 11