September 26, 2013

Java 8 Developer Preview

I'm sure 99% of developers don't use the bleeding edge, but my curiosity got the best of me and I decided to see what does Java 8 really mean. The big thing about Java 8 is that it means less boilerplate code and performance increases without having to write much code at all.

Edit:

If you would like to try out Java 8 w/ Eclipse for yourself, there is only two easy steps:

  • You will need to download and install the JDK8 developer preview from here.
  • Get a bleeding edge Eclipse build from here. (Note: this is a preview and likely will have a few glitches).


However, I don't get to have candy and eat it because Android doesn't support anything past Java 6. It is actually kinda depressing because a ton of things I like about Scala are being added into Java 8. Let's do a quick comparison of what has been added after Java 6 (this is by no means complete):

Java 7

  • Try-with-resources, basically the equal to 'using' blocks from C#. If you aren't familiar, think of them as automatically closing of resources/streams/etc.
  • String switch statements, before you were limited to integer types and enums. C# has had this one for a while and it also came with Java 7.
  • ForkJoinPool, a concurrency method to support load balanced parallelism by allowing thread pools to steal work from other busy threads.
  • ThreadLocalRandom, a random generator which is thread-safe!
  • New Java Path features, fixes up some major issues with Java and file systems (such as path normalization across platforms, symbolic links, etc).
  • Multiple catch statements, reducing the number of catch clauses.
  • Type inference on generics!
(A few things are missing from the above, but I really don't need to make this longer)

Java 8
  • Lambda Expressions, basically a dynamic invoke of a method which uses type inference for the arguments and allows you to create closures with them. It really simplifies the language and removes the need for anonymous classes! 
    • Ex: Arrays.sort(x, (s1, s2) -> s1.charAt(0) - s2.charAt(0));
    • s1 and s2 are Strings (type inference), you sort them based on the value of the first char.
  • Method References, create a lambda and pass it around like an object. Who wouldn't want that!
  • Default Methods, create stubs or default implementations in your interfaces. This is pretty huge as it basically allows Java to do some 'smart' multiple inheritance and allows you to write a single implementation common to multiple classes.
  • Streaming API, functional operators for collections (or even arrays) in Java. Think of it as LINQ for Java if you come from C#. It also performs lazy evaluation to make life even better.
  • Parallel Collections, want to make your sorting parallel? want to perform a bunch of actions on a collection in parallel? You can now do this via the streaming API to basically make your code parallel without actually writing anything special for it.
    • myList.parallelStream().forEach((s) -> System.out.println(s)); // Parallel
    • myList.stream().forEach((s) -> System.out.println(s)); // Serial
  • New JavaScript engine built-in which offers near V8 level of performance (from what I've heard).
  • Removed PermGen space from JVM (most people have no idea what this is). Basically means less configuration of the JVM when running applications.
  • A non-internal API for Base64 encode/decode (Long over due).
  • New Date & Time API, based on Joda Time!
  • Statically-Linked JNI Libraries!
(Again, a few things are missing from the above, but I really don't need to make this longer)

That list is pretty big when comparing it to Android's Java. I believe there are a few ways to get some of the Java 7 features working on Android, but I know almost everything in Java 8 is very likely to not work with Android. Scala 2.11 is targeting Java 6, in part because of Android, which means they will not be able to gain any significant performance improvements provided by the latest version of Java. I would expect 2.12 will be targeting Java 8 and hopefully so will Android by that time.

What I hope is in Java 9:
  • Full type inference! I really would love to seem something like var/val from Scala or even just var from C#. It is pretty clear not all types need to be visibly shown for all variables and the compiler can infer most of them just fine. Lambda is a step in the right direction and shows they are starting to do it, but really this should just happen for the entire language.
  • Default parameters and named parameters. When you use this in other languages it feels so clunky in Java to not be able to specify defaults, or have to ensure the ordering of parameters with no hints as to what parameters you are actually using. Overloading is very annoying because of this and could really improve the language as a whole.
  • Reified Generics, I'm tired of dealing with type erasure. It doesn't bother me for 99% of the things I am working on, but the odd 1% I do it really bugs me.
  • GPU accelerated Java, I know they are working on it!
  • An improved JNI interface, the current system is pretty harsh and it really doesn't need to be.
  • There has been talk about improving the built-in collections to be more efficient and handle large data.
  • I would also hope they would improve the built-in collections library to support additional types of collections. I.E. Guava Collections and Apache Commons Collections. I would also really like to see some built in support for Graphs, JUNG is nice and all but there really should be something built in for this fairly common data structure.
  • Implicits or method extensions would be cool as they really enhance a language, but I could live without this. 
Ah well, I can continue to dream... 

No comments :

Post a Comment