Core Java

Java SE 8 new features tour: The Big change, in Java Development world

I am proudly one of the adopt-OpenJDK members like others professional team members but joined from last 8 months, and we went through all stages of Java SE 8 development, compilations, coding, discussions … etc., until we bring it to the life. And it is released on March 18th 2014 and it is now available for you.

I am happy to announce about this series “Java SE 8 new features tour”, which I am going to write it provided with examples to streamline the Java SE 8knowledge gaining, development experience, new features, and APIs that will leverage your knowledge, enhancing the way you code, and increase your productivity as well. I hope you enjoy it as I am doing when writing it.

 
We will take a tour of the new major and important features in Java SE 8 (projects and APIs), the platform designed to support faster, and easier Java development. We will learn about Project Lambda, a new syntax to support lambda expressions in Java code.

Checking the new Stream API for processing collections and managing parallel processing.

Calculating timespans with The DateTime API for representing, managing and calculating date and time values.

In addition to Nashorn, a new engine to better support the use of JavaScript code with the Java Virtual Machine.

Finally, I will also cover some lesser-known features such as new methods for joining strings into lists and other more features that will help you in daily tasks.

For more about Java SE 8 features and tutorials, I advise you to consult the Java Tutorial the official site and Java SE 8 java API documentation too.

The topics we are going to cover during this series will include:

  1. Installing Java SE 8, notes and advices.
  2. Introducing Java SE 8 main features, the big change.
  3. Working with lambda expressions and method references.
  4. Traversing collections with streams.
  5. Calculating timespans with the new DateTime API
  6. Running JavaScript from Java with Nashorn.
  7. Miscellaneous new features and API changes.
  1. Installing Java SE 8, notes and advices.

    1. Installing Java SE 8 on Windows

      In order to run Java SE 8 on Microsoft Windows, first check which version you have. Java SE 8 is supported on Windows 8, 7, Vista, and XP. Specifically, you’ll need these versions. For Windows 8 or 8.1, you’ll need the desktop version of Windows. Windows RT is not supported. You can run Java SE 8 on any version of Windows 7, and on the most recent versions of Windows Vista and Windows XP. On Server based versions of Windows, you can run 2008 and the 64-bit version of 2012.If you want to work on Java Applets you’ll need a 64-bit browser, these can include Internet Explorer 7.0 and above, Firefox 3.6 and above, and Google Chrome which is supported on Windows, but not on Mac.You can download the Java Developer Kit for Java SE 8 from

      1. URL java.oracle.com That will take you to the current Java home page.
      2. Click Java SE.
      3. Under Top Downloads. Then click the Download link for Java 8.
    2. Installing Java SE 8 on Mac

      In order to work with Java SE 8 on Mac OS X, you must have an Intel-based Mac running Mac OS X 10.7.3, that’s Lion, or later. If you have older versions of Mac, you won’t be able to program or run Java 8 applications. In order to install Java SE 8 you’ll need administrative privileges on your Mac. And in order to run Java applets within a browser you’ll need to use a 64 bit browser, such as Safari or Firefox.Google Chrome is a 32 bit browser, and won’t work for this purpose.As described earlier on installing Java SE on windows, the same website has the MAC OS .dmg version to download and install. Actually contains all operating systems versions. However, our focus here would be on windows and MAC.

    Now you’re ready to start programming with Java SE 8 on both Windows and MAC OS X platforms.

    After we have installed Java SE 8 probably, let’s dive into the first point and have a look at Java SE 8 main features in a nutshell, to begin our coding tour on our favorite IDE.

  2. Introducing Java SE 8 main features, the big change.

    An overview of the JSR 337: Java SE 8 Release Contents

    Java SE 8 is a major release for the Java programming language and the Java virtual machine. It includes many changes. Some have gotten more coverage than others like Lambda expression, but I’m going to talk about both the major changes and a few of the minor ones.

    JSR 335: Lambda Expressions

    Probably the most attention has gone to Project Lambda, a set of new syntactical capabilities that let Java developers work as functional programmers. This includes lambda expressions, method references and a few other capabilities.

    JSR 310: Date and Time API

    There is a new API for managing dates and times. Replacing the older classes. Those older classes are still in the Java Runtime, but as you build new applications, you might want to move to this new set of capabilities, which let you streamline your code and be a little more intuitive in how you program. There are new classes to manage local dates and times and time zones and for calculating differences between different times.

    The Stream API

    Adds new tools for managing collections including lists, maps, sets, and so on.A stream allows you to deal with each item in a collection without having to write explicit looping code. It also lets you break your processing into multiple CPUs. So, for large, complex data sets you can see significant performance improvement.

    Project Nashorn

    The Nashorn JavaScript engine is new to Java SE 8 too. This is a completely new JavaScript engine written from scratch that lets you code in JavaScript but lets you integrate Java classes and objects.Nashorn’s goal is to implement a lightweight high-performance JavaScript runtime in Java with a native JVM. This Project intends to enable Java developers embedding of JavaScript in Java applications via JSR-223 and to develop freestanding JavaScript applications using the jrunscript command-line tool.In the article on Nashorn, I’ll describe how to run Nashorn code from the command line. But also how to write JavaScript in separate files, and then execute those files from your Java code.

    Concurrency API enhancements.

    There are also enhancements to the concurrency framework, which lets you manage and accumulate values in multiple threads. There are lots of smaller changes as well.

    String, numbers has new tools

    There are new tools for creating delimited lists in the string class and other new classes. There are tools for aggregating numbers including integers, lungs, doubles, and so on.

    Miscellaneous New Features

    There are also tools for doing a better job of detecting null situations, and I’ll describe all of these during the series.

    And I’ll describe how to work with files, using new convenience methods.

So, when is Java SE 8 available?

The answer is, now. It was released on March 18, 2014. For developers who use Java to build client site applications, the JavaFX rich internet application framework supports Java 8 now. And most of the Java enterprise edition vendors support Java 8 too. Whether you move to Java SE 8 right away depends on the kinds of project you’re working on.

For many server and client site applications, it’s available immediately.

Not for Android yet.

Android developers beware; Java SE 8 syntax and APIs are not supported in Android at this point.

It’s only very recently that Android moved to some of the newest Java 7 syntax. And so, it might take some time before Android supports this newest syntax or the newest APIs. But for all other Java developers, it’s worth taking a look at these new capabilities.

What about IDEs?

Java SE 8 is supported by all of the major Java development environments. Including Oracle’s Netbeans, Intellij Idea, and Eclipse. For this series I’ll be doing all of my demos in Netbeans, using Netbeans, version 8, which available to download from https://netbeans.org/downloads/.

However before we start diving into this series, let’s check first, that we have installed Java SE 8 probably and start a new project under Netbeans, which will contains all code that we are going write. Then develop a lambda code to test our project if it is working or not probably with Java SE 8 .

Alternatively you can download the series source code from my Github account, open it with Netbeans and follow what I am showing next, and in upcoming series code.

Project on Github: https://github.com/mohamed-taman/JavaSE8-Features

Hello world application on Java SE 8 with Lambda expression.

Steps (not required if you navigating my code):

  1. Open NetBeans 8 –> from file –> New project –> from left, and choose Maven –> from right, and choose Java Application –> Click next.
  2. Follow the following screen shoot variables definition, or change to your favorite names and values –> then click finish.
     
    JDK8-SES-P1-1
  3.  

  4. If everything is okay you should have the following structure, on project navigator:
     
    JDK8-SES-P1-2
  5.  

  6. Click on Project “Java8Features” –> Click File, from upper menu –> then, Project properties.
  7. Under Category –> From left choose Source, then check that “Source/ Binary format” is 1.8. –> From left open Build, and choose Compiler, then check that “Java Platform” is pointing to your current JDK 8 installation –> Click Ok.
  8. If JDK 8 not presents then go to tools –> chooses, Java Platforms –> Add Platform –> Then chooses Java Standard Edition –> then point to your installed JDK 8.
  9. Now our project configured to work with Java 8 so let’s add some Lambda code.
  10. On Package “eg.com.tm.java8.features”, right click, and select New from menu –> Java Interface –> Name it Printable, under overview package “eg.com.tm.java8.features.overview” –> click finish.
  11. Implement Printable interface as the following:
      /*
     * Copyright (C) 2014 mohamed_taman
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
     
    package eg.com.tm.java8.features.overview;
     
    /**
     *
     * @author mohamed_taman
     */
    @FunctionalInterface
    public interface Printable {
         
        public void print();
    }
  12. On the same package add the following class named “Print”, with main method as the following:
    /*
     * Copyright (C) 2014 mohamed_taman
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
     
    package eg.com.tm.java8.features.overview;
     
    import static java.lang.System.out;
    /**
     *
     * @author mohamed_taman
     */
    public class Print {
         
        public static void main(String[] args) {
            
            Printable job = ()-> out.println("Java SE 8 is working "
                                            + "and Lambda Expression too.");
             
            job.print();
        }
         
    }
  13. Right click on Print class and choose Run. If every thing is okay then you should see the following output.
    ------------------------------------------------------------------------
    Building Java8Features 1.0-SNAPSHOT
    ------------------------------------------------------------------------
    --- exec-maven-plugin:1.2.1:exec (default-cli) @ Java8Features ---
    Java SE 8 is working and Lambda Expression too.
    ------------------------------------------------------------------------
    BUILD SUCCESS
  14. Congratulation your Java SE 8 project works fine, let’s explain what we have written.

    Most of this code would work on Java 7, but there’s an annotation here that was added in Java SE 8, FunctionalInterface. If your Netbeans environment isn’t correctly configured for Java 8, this annotation will cause an error because it won’t be recognized as valid Java code. I don’t see an error, so that’s a good sign that Eclipse is working as I hoped.

    Next I’ll open this class definition named Print.java. This is a class with a main method so I can run it as a console application and it has a critical line of new Java 8 syntax.

    It’s creating an instance of that functional interface I just showed you using a lambda expression, a style of syntax that didn’t exist in Java prior to Java 8. I’ll explain what this syntax is doing very early in the next article.

    But all you need to know right now is that if this code isn’t causing any errors, then once again, Netbeans is recognizing it as valid Java syntax. I’m creating an instance of that interface and then calling that interface’s print method. And so, I’ll run the code.

    I’ll click the Run button on my tool bar and in my console I see a successful result. I’ve created an object, which is an instance of that interface using a lambda expression. And I’ve called its method and it’s outputting a string to the console. So, if this is all working, you’re in great shape. You’re ready to get started programming with Java SE 8 in Netbeans. If you had any problems along the way, go back to earlier steps and walk through the steps. One step at a time.

Resources:

  1. The Java Tutorials, Lambda Expressions
  2. JSR 310: Date and Time API
  3. JSR 337: Java SE 8 Release Contents
  4. OpenJDK website
  5. Java Platform, Standard Edition 8, API Specification
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button