“What
the hell is he up to?” You may think. “Why on earth is he going to discuss the
animals over here? Is he out of his mind?
What the f..” Wait. Before you go
too far down that road and possibly plan my murder, let me clarify this. We are
not going to discuss about a mammal and an arachnid here. Dolphin and Spider
are the code names of Java 7 and Java 8 respectively. Although code names have
been dropped since java 8, spider is the known name among the Javaites.
Java 7 was released on 28th July 2011 and Java 8 was released on 18th March 2014. Even though Java 7 was released 2 and half years ago, everyone hasn’t migrated to it. People are gradually migrating to it though. If I talk about my team then, we are still evaluating Java 7 and its features and compatibility of our application to it and are planning to migrate in a month or two. Hence, going by this way, I think it will take very long for people to switch onto Java 8, and it isn’t supported on windows XP (There are workarounds though) which I believe, will be a significant factor in delay. But the features it brings are commoving. Let’s discuss them. We will discuss some of the features of Java 7 and 8. One more thing, we will not discuss the language changes (e.g. Strings in switch in Java 7), because, well, every Tom, Dick and Harry is aware about those. We will discuss the features/APIs which are added in Java platform.
Java 7 features:
Java 7 was released on 28th July 2011 and Java 8 was released on 18th March 2014. Even though Java 7 was released 2 and half years ago, everyone hasn’t migrated to it. People are gradually migrating to it though. If I talk about my team then, we are still evaluating Java 7 and its features and compatibility of our application to it and are planning to migrate in a month or two. Hence, going by this way, I think it will take very long for people to switch onto Java 8, and it isn’t supported on windows XP (There are workarounds though) which I believe, will be a significant factor in delay. But the features it brings are commoving. Let’s discuss them. We will discuss some of the features of Java 7 and 8. One more thing, we will not discuss the language changes (e.g. Strings in switch in Java 7), because, well, every Tom, Dick and Harry is aware about those. We will discuss the features/APIs which are added in Java platform.
Java 7 features:
- File I/O library: Java 7 contains Java.NIO 2.0 package (NIO stands for non-blocking I/O). It contains different classes which help you play around with file systems very easily. The ways to access the file have become very much compact and optimized (i.e. you can read the file in just 2 lines of code rather than the old school method of creating a file object, then opening file input stream then reading the file line by line or so). This package even provides the facility using which you can create/access the custom file systems. One of the examples is Zip file provider, which can treat the zip file as a file system and allows you to access the different files from it. Isn’t this awesome? Well, for deeper dive into this, I would recommend to visit this link.
- Use of Timsort algorithm: : In previous versions, merge sort was used to
sort the arrays. Merge sort works by splitting the main array into multiple
arrays (each containing 1 element), and then merge them and sort them to
finally produce the array which is in sorted form. Timsort was invented in 2002.
It works in a different way. It first searches the array for the segment which is
already sorted. It removes such sub array from main array (to make the sorting
of remaining elements easier). It against performs same task on the
remaining elements. Such iteration is called a ‘run’.
Although both merge sort and Timsort have equal worst case complexity (O(n logn)), Timsort provides faster best case performance (O(n)) compared to that of merge sort (O(n logn)). Also, data analysts assume that, in real time systems, an array contains many such sub arrays which are already in sorted form, making the task of Timsort easier. Hence, in Java 7, it is utilized. More on this is described here.
- Support for dynamism: First, let us discuss what dynamic language is. A dynamic language is the one that can exhibit the behavior at runtime which the other languages can exhibit at compile time only. Let’s have an example to simplify this. Java is a static language, meaning, the operations like type checking of methods (e.g. a method with an integer argument can’t be called using string argument) happen at compile time. Once the compilation is performed and byte code is generated, it is done and dusted. You can’t modify this method at runtime. Whereas in dynamic languages (like Python, Ruby), it is possible. You can modify the behavior of method at runtime; you can even make a class extend another at runtime. Due to this, there has been an increasing pressure on Java creators to include dynamism in Java and hence, they have included some of it in Java 7. This may be a long shot for us to understand; however, to get more idea of it, I would recommend visiting this link.
Java 8 features:
- Support for Lambda expressions: This is the feature I was waiting so eagerly to
discuss about. This provides those powers to Java that it lacked compared to the
other high level languages. It provides the wings of functional programming to
Java. Let us discuss about functional programming first.
Functional programming is a programming in which the functions (and not objects) are basic building blocks of a program. It helps very much in reducing the complexity of the language and makes tackling complex problems easier. Javascript is a functional language. Example of the same is provided here.
Now, as we all know, Java is an object oriented language (except for the primitives, obviously). Each and everything in Java has to be accessed through the objects and classes (in case of statics). The methods (functions) cannot live on their own; they must be called using either objects or classes. Hence, we can say that functions are not important in Java, or, to put it in the other words, functions are less important than objects in java.
In any functional language, functions are THE most important parts. I would even say that functions are the VIPs of any functional language. The most important feature of any functional language is ‘Closure’, which, according to wiki, is a function or reference to a function together with a referencing environment—a table storing reference to each of the non-local variables of that function. Sounds too complex, doesn't it? Anyway, let’s not go much into this definition. Let’s talk about how java achieves this purpose.
The feature in Java 8, which is similar (or let’s say, closest) to closure is, Lambda Expressions (LE). An LE basically means a function without declaration (similar to anonymous inner class). Following are the examples of LE:
(int a, int b) -> {return a+b;}() -> System.out.println("Hello World");
Doesn’t this seem interesting? Furthermore, LE can also be assigned to the reference of a functional Interface. A functional interface is an interface which contains just 1 method (e.g. Runnable). Following are examples of LE assigned to reference of functional interfaces:Runnable r = () -> System.out.println("hello world");new Thread(() -> System.out.println("hello world")
).start();
This way, we can start a new thread without even overriding the run() method (we have LE’d the run() method here).
Using an LE, we can even add default and static methods to the interfaces with concrete implementations. One can write a book on LEs, however, we will wind our discussion up regarding LE. If you want to study more, you can visit this link. - Removal of Permanent Generations: Well well well, this also sounds like a commoving feature. Doesn't it? Here, we assume that everybody is aware about the permanent generation. If you don’t then, I would strongly recommend you to read this blog of mine first and come back. Go ahead, push this line in your stack, go and read the blog, pop this line back and continue reading.
Back to our point, this feature sounds like living without the limits. This means you will no longer be haunted by the demon called ‘java.lang.OutOfMemoryError: PermGen space’. What a respite it is! The arguments named PermSize and MaxPermSize will be ignored. A warning will be issued if they are present, saying that Java doesn’t give a damn about them.
JVM of Java 8 will be using native memory to store the classes’ data. That memory is called Metaspace. Native memory simply means memory available to Java process and is controlled by OS. However, using a parameter named MaxMetaspaceSize, we can limit the memory usage. If we don’t specify, Metaspace will grow dynamically at runtime depending upon usage. More on this is explained here. - Date and Time API: Although existing versions of Java provide a bunch of APIs to play around with Date and Time, handling of these has always been tricky. Sometimes, we may need to write our own code to achieve the expected result even though there are plenty of methods available (one of the prime examples of it, that I faced recently, was to compare two calendar objects without considering time).
As a part of Java 8, a whole new bunch of Date and Time APIs will be introduced into the mix (the means, now we will have two bunches). It will provide a lot more flexibility to manipulate Date and Time. We will not discuss these in more details as there are just classes and methods to get used to (in short, nothing commoving). However, if you want to study, you can study here. Also, as per readme file of these specifications, these APIs are not incomplete and may get changed later.