DEV Community

Cover image for Java 7 -> Java 8
ABHINAVA GHOSH (he/him)
ABHINAVA GHOSH (he/him)

Posted on

Java 7 -> Java 8

We all as Java Developers will agree that Java 8 is the best upgrade that Java/Oracle pushed till now.
Although the latest version of Java is Java 14 but still the most used version of Java is Java 8.

So in this post I will be discussing the major changes that were pushed by the "Java Creators" from transitioning from Java 7 - > Java 8.

❗ [DISCLAIMER]-This will be a long post , because i wanted it to be the single core resource for Java 7 -> Java 8 transition, which will help developers in the future as they get everything in one single post and do not have to refer to any other resource .

So sit tight and enjoy !! ✨


πŸ“‹ Here we mainly discuss the differences, or in other words advancements that have been included in Java 8 when compared to Java 7.
To begin with, let me give a brief introduction to Java 7. Java 7 was released in 2011. This was the first major update of Java after being acquired by Oracle from Sun Microsystems. Java 7 is the final Java version that would officially support Windows XP.

πŸ“Ž There were some major features that were included in Java 7. Such as:

  • Compressed 64-bit pointers.
  • Upgraded class-loader architecture.
  • Multiple exception handling.
  • JVM support for dynamically support languages.
  • Upgraded Rowset 1.1 and JDBC 4.1.
  • String object in a switch statement.
  • Automatic resource management in a try statement and much more.

πŸ“’ But in this blog, our main concern will be on discussing a few advancements that happened when updating from Java 7 to Java 8.
Java 8 was released in March 2014. The most anticipated feature β€” Lambda Expressions, was introduced and made possible from Java 8. The use of this Lambda Expressions made Java closer to functional programming which it was not used to be. Java 8 also includes a faster and improved JVM which makes it possible to run programs more efficiently.

πŸ“Ž Now it is time to point out and discuss some of the major upgrades of Java 8 when compared to Java 7.

  • Lambda Expressions
  • Lambda expressions only have a body and a parameter list.
  • Advantages we get by using Lambda Expressions are,
  • Enhanced iterative syntax.
  • Readability.
  • Code Reuse.
  • JAR file size reduction.
  • Simplified variable scope.
  • Encouraging Functional Programming.

Let us look at a couple of examples using lambda expressions:-

image
lambda expression with no arguments passed ☝️


image
Lambda Expression with One Arguments Passed ☝️


image
Lambda Expression with Two Arguments Passed ☝️


In Java, a functional interface is an interface that has only one abstract method.

🎯 Null Reference Template
We can also call it the Java Optional Class.

  • Advantages using the Java Optional class are,
  • No Null Pointer Exceptions at run-time.
  • Clean APIs can be developed.
  • Null checks are not required.

image
Java Optional Class Example ☝️


🎯 New Date and Time API

In java.util.Date class the years starting from 1900 and the months start from 0. So it is important to know it, as when we want to assign a year and a month it must be as (YYYY-1900) and (MM-1). This was overcome in Java 8 using the classes in java.time package.
Unlike integer or string, the Date class is mutable. Which means that the date can be changed without the owner class knowing. Java 8 has a solution for this. The LocalDate, LocalTime, LocalDateTime, etc. introduced in Java 8 are immutable (The original object is not changed. Whenever a change is done, a new object will be returned).

image
Use of the New Date and Time API ☝️


🎯 Unsigned Integer Arithmetic

It is not clear what Java 8 has tried to achieve by the advancement in unsigned integer arithmetic. But generally, an unsigned integer is considered to be harmful to Java. It adds complexity to the type conversion tools, type system, and library APIs.

image


🎯 Stream API

The main advantage that we get using the Stream API is that it clearly increases the speed and efficiency of the program. It is considered to be a powerhouse in Java.
image


🎯 Parallel Sorting

Parallel sorting uses multiple threads for the operation. It makes the program fast and efficient. This will not be that much visible with a small data set. But if we use large data set the efficiency and the increase in performance will be highly noticeable.
Image

Credits-@Chinthaka Jayatilake .


🎯 New JavaScript Engine (deprecated after Java 11)

The JavaScript Engine introduced with Java 8 was named Nashorn.
With that now, we have come to the end of this section and the main aim of it was to give an idea about the updated and added features in Java 8 compared to Java 7. According to the examples provided hope you got an idea on how to use these advancements for your benefit. Most of these provide us with advantages but there are certain drawbacks as well if we look at different perspectives. It is important to remember, only to absorb the features that would help us and make our program more efficient, reliable, and stable.


Now lets go into a little deeper into some important concepts that are used very often.

Java 8 method references are the shortened versions of lambda expressions calling a specific method. For example, lambda expression (Student s) -> s.getName() which is calling a method getName() of Student class can be shortened as Student::getName using Java 8 method references. Let’s see them in detail.


🎯 Java 8 Method References :

πŸ“Œ Definition:-
Java 8 method references can be defined as shortened versions of lambda expressions calling a specific method. Method references are the easiest way to refer a method than the lambdas calling a specific method. Method references will enhance the readability of your code.

πŸ“Œ Syntax

πŸ“Ž Method reference to static method :

ClassName::MethodName

Use this syntax when you are referring to a static method.

Integer::parseInt, Math::max

πŸ“Ž Method reference to instance method of an existing object :

ReferenceVariable::MethodName

Use this syntax when you are referring to an instance method of already existing object.

s::getName

where β€˜s’ is a reference variable referring to Student object which already exist.

πŸ“Ž Method reference to instance method of non-existing object :

ClassName::MethodName

Use this syntax when you are referring to an instance method by passing reference variables as an argument.

(Student s) -> s.getName() can be written as
Student::getName


πŸ“Œ Constructor References

You can also refer to the constructor of a class same as method references. Syntax for referring to constructor is:

ClassName::new

image


🎯 Java 8 Interface Changes : Default Methods And Static Methods

It has been a tough task for Java API developers to add new methods to current interfaces. Because, even if you add a single abstract method to an interface, all existing implementations of that interface have to be updated with implementation of new method. What if there exist hundreds or thousands of implementations of an interface? Even worse, What if you don’t have control over all of those implementations? To overcome such overhead, new features are introduced to interfaces from Java 8. They are default methods and static methods. In this article, we will see these new Java 8 Interface Changes.


🎯 Java 8 Interface Changes

From Java 8, interfaces can also have concrete methods i.e methods with body along with abstract methods. This is the major change introduced to interfaces in Java 8 to help Java API developers to update and maintain the interfaces. The interfaces can have concrete methods either in the form of default methods or static methods.


🎯 Default Methods

Definition With Example:-

Default methods of an interface are the concrete methods i.e methods with body for which implementing classes need not to give implementation. They inherit default implementation. Default methods start with the modifier default.

interface InterfaceWithDefaultMethod
{
    void abstractMethod();           //Abstract Method

    default void defaultMethod()
    {
        System.out.println("It is a default method");
    }
}

class AnyClass implements InterfaceWithDefaultMethod
{
    @Override
    public void abstractMethod() 
    {
        System.out.println("Abstract Method implemented");
    }

    //No need to implement defaultMethod()
}
Enter fullscreen mode Exit fullscreen mode

🎯 Why Default Methods?

Default methods are introduced to add extra features to current interfaces without disrupting their existing implementations. For example, stream() is a default method which is added to Collection interface in Java 8. If stream() would have been added as abstract method, then all classes implementing Collection interface must have implemented stream() method which may have irritated existing users.

Thanks to Java 8 default method feature, now it is a default method, all implementations of Collection interface inherit default implementation of stream() method.


🎯 Three Rules To Solve Diamond Problem

In Java, a class can extend only one class but can implement multiple interfaces. With the introduction of default methods, it is possible that your class inherit multiple methods with same signature. In such scenarios, to solve the conflict, Java 8 specifies 3 rules to follow.

image


🎯 Static Methods

Definition With Example
From Java 8, interfaces can also have static methods. Static methods are also concrete methods but they can’t be implemented.

interface InterfaceWithDefaultAndStaticMethod
{
    void abstractMethod();           //Abstract Method

    default void defaultMethod()
    {
        System.out.println("It is a default method");
    }

    static void staticMethod()
    {
        System.out.println("It is a static method");
    }
}

class AnyClass implements InterfaceWithDefaultAndStaticMethod
{
    @Override
    public void abstractMethod() 
    {
        System.out.println("Abstract Method implemented");
    }

    //No need to implement defaultMethod()

    //Can't implement staticMethod()
}
Enter fullscreen mode Exit fullscreen mode

🎯 Why Static Methods ❓

Do you know? Collection and Collections.

Collection is an interface and Collections is an utility class containing only static methods which operate on Collection objects.

Java API developers have followed this pattern of supplying an utility class along with an interface to perform basic operations on such objects. But from Java 8, they have break this pattern by introducing static methods to interfaces.

With the introduction of static methods to interface, such utility classes will disappear and methods to perform basic operations will be kept as static methods in interface itself.


🎯 Interface Vs Abstract Class After Java 8

With the introduction of concrete methods (default and static methods) to interfaces from Java 8, the gap between interface and abstract class has been reduced significantly. Now both can have concrete methods as well as abstract methods. But, still there exist some minute differences between them. In this article, we will try to list down the differences between interface Vs abstract class after Java 8.

Differences Between Interface And Abstract Class After Java 8 :
🎯 Fields
Interface fields are public, static and final by default. Interfaces still don’t support non-static and non-final variables. Interfaces can only have public, static and final variables. On the other hand, abstract class can have static as well as non-static and final as well as non-final variables. They also support private and protected variables along with public variables.


🎯 Methods
After Java 8, an interface can have default and static methods along with abstract methods. Interfaces don’t support final methods. But, abstract classes support final as well as non-final methods and static as well as non-static methods along with abstract methods.

Also note that, only interfaces can have default methods. Abstract classes can’t have default methods.


🎯 Constructors
Interfaces can’t have constructors. Abstract classes can have any number of constructors.


🎯 Member’s Accessibility
All members of interfaces are public by default. Interfaces don’t support private and protected members. But, abstract classes support all type of members – private, protected and public members.


🎯 Multiple Inheritance
A class can extend only one abstract class, but can implement multiple interfaces. Thus, a class can inherit multiple properties from multiple sources only through interfaces, not through abstract classes.


🎯 Interface Vs Abstract Class After Java 8 :
The below table and program summarizes the similarities and differences between interface and abstract class after Java 8

image

interface anyInterface
{
    int i = 10;            //By default, interface fields are public, static and final

    void abstractMethod();          //Interface can have abstract method

    default void defaultMethod()
    {
        System.out.println("Interface can have default method");
    }

    static void staticMethod() 
    {
        System.out.println("Interface can have static method");
    }

    //No constructors in an interface

    //No non-static and non-final variables in an interface

    //No private fields and methods in an interface

    //No protected fields and methods in an interface

    //No final methods in an interface
}

abstract class anyAbstractClass
{
    private int a;          //Abstract class can have private field

    protected int b;        //Abstract class can have protected field

    public int c;           //Abstract class can have public field

    static int d;           //Abstract class can have static field

    final int e = 10;       //Abstract class can have final field

    int f;                  //Abstract class can have non-static and non-final field

    public anyAbstractClass() 
    {
        System.out.println("Abstract class can have constructors");
    }

    abstract void abstractmethod();    //Abstract class can have abstract method

    private static void staticMethod() 
    {
        System.out.println("Abstract class can have private and static method");
    }

    public void nonStaticMethod()
    {
        System.out.println("Abstract class can have public and non-static method");
    }

    protected void protectedMethod() 
    {
        System.out.println("Abstract class can have protected method");
    }

    final void finalMethod()
    {
        System.out.println("Abstract class can have final method");
    }

    //No default method in an abstract class
}
Enter fullscreen mode Exit fullscreen mode

References:

Oracle Docs

Other resources


Thank you for reading this Article. 😊

Please leave a ❀️ if you liked it.
An πŸ¦„ will be great.

And let me know in the discussions area ,if you have any suggestions for me.

Have a Good Day! πŸ˜ƒ

Some of my other posts:

Concept link
Java Access Modifiers goto Article
Java Generics goto Article
Java Regex goto Article
Java Streams Api goto Article

Top comments (3)

Collapse
 
tusharpandey13 profile image
Tushar Pandey

With the release of Java 11, Nashorn is deprecated, and will likely be removed from JDK 15 onwards.[10][11] The GraalVM was suggested as a replacement.

From Wikipedia.
If nashorn is being deprecated, why is it counted in new? Also, it has support upto es5 only. I guess that's a bit dated.

Collapse
 
the_unconventional_coder profile image
ABHINAVA GHOSH (he/him)

Updated.
Thanks.

Collapse
 
musman07 profile image
M-Usman07

cuitutorial.com/courses/programmin...
Java 7 (codename β€œDolphin”) was the first major update to the Java programming language under the ownership and stewardship of Oracle since it acquired Sun Microsystems. The final acquisition was completed by Oracle Corporation on Jan 27, 2010. The American technology giant hosted a global event to celebrate the launch of Java Standard Edition 7. The launch of Java SE 7 was an affirmation of the Oracle’s commitment to the language and technology. It was a big achievement for the tech giant’s two-year ownership of Sun Microsystems – the company that created Java programming language.

Read more: Difference between Java 7 and Java 8 | Difference Between differencebetween.net/technology/d...