Java 8 is one of the Java versions that brought with it some new features that are considered as revolutionary for computer science and coders (developers) using this language. It is considered the major version released for java after the java 5. Java 8 was released on March 18th, 2014. It contains a huge number of new features and upgrades to ease the use of this programming language and improve the programming technics and security for their users.
The major features include in this new release of java are: The Lambda expressions, concurrent accumulator, Method references, default method, new stream API, new Date & time API, The ability to remove the permanent generation…etc
In this introductory chapter, we’ll talk about those major new features added to the Java Programming Language.
The lambda expressions
For a Java developer, the lambda expressions represent one the most incredible new introduced features / add-on by the ones who developed this new version. It allows us to clarify the syntax and by the way the whole code. Using the lambda expressions, the developer is able, now, to code local functions that can be used as returned value or passed as argument. It takes the place of the anonymous function had no name, neither a class in the previous versions.
To be clearer, there is an example about how we were coding before java 8 and now:
From this example, we notice that using the lambda expression provides us a more clear code and less stuffed
We will speak about this new feature with more details in a proper chapter for them.
It is a more or less common habit to use the numeric counters in solutions based on the Java language, counters accessed by multiple threads.
With Prior Java 8 versions, modifying those counter values were really hard to realize, but with this new version, Java 8 now developed an issue to this problem with its accumulator classes, the values now can go up or down in a thread safe method.
So, what’s a stream? I prefer not using the official definition which is, for me, too abstract and confused. A stream is a new abstract layer, it can provide us a set of elements in a constructive manner, for a particular type of course. A stream gives us the opportunity to write a code having a high level of abstraction by using a functional approach.
The Stream feature allows us also to use data in a declarative way without running a whole iteration, so it will make our code faster to be executed (compiled).
The stream API feature allows us to do a lot of operations like slicing, filtering, mapping, matching, finding…etc those are methods that will give us the possibility to deal with a lot of the processing queries of complex data. We can also use some primitive methods for primitive elements which contains some additional operations such as max, min, sum…etc
The Stream API is considered as being one of the best features for developers working on Big data field or collection issues. Their running can be sequential [stream () method] or parallel [parallel stream ()method]
As an example, we will use one of those methods which is the filtering stream:
This example consists of a code that allows us to find the words starting with “B” or “M”
This feature allows us to apply a type of an annotation, the same one, more than once for the same declaration or type use. Something that was not possible in the previous versions. And to do this we have to group all of them in a single container annotation
Default and Static Methods
One of the greatest features added to this java version is the default & static interface methods, even though, the static method was included in the older versions, the default one is the new one here.
So, the default method allows us to add new functionalities to the libraries ‘interfaces, and by this to ensure a certain binary compatibility for codes written with prior versions. We will see through this example the real explanation of this methods
In the example above, we see an introduction to a test with those two distinct methods, till now there is no difference that could be observed. (You should follow this example and write it in your own IDE)
Even now, when trying to implement the test interface, the IDE won’t complain because the implementation is already done.
As we can see here, the default method is invoked / called, like any other instance method, and same thing for the static one which is invoked like any other static/interface method
But what would happen if we try to override both methods? That’s what we’ll see right below:
When we override the default method, the code will run as normal as possible but the static one will display an error message, why? Because the static method doesn’t support the overriding.
For more examples, and deeper explanation,please wait for our next article which will be a whole chapter about this feature.
Java 8 and Android Studio
One of the big questions asked are if this new feature of Java 8 are all included while using Android studio, and supported by the android system?
Of course it is possible, but we have also to configure the Gradle file by entering the following lines in the build.gradle file:
And also, to add the Jack toolchain to use the new features of java 8.
Hope this article will enhance your will to develop your skills in java, and to adopt the java 8 new features, because they are here just to help you improve your coding skills and make your code clearer.