Java 7 -> Java 8 
Tags: career, java, beginners, tutorial
Published: 2020-08-23 16:42:38


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.


 :exclamation: **[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 !! :sparkles:

 ---


 :clipboard: 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.

:paperclip: 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.

 :ledger: 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.

:paperclip: 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](https://miro.medium.com/max/874/1*m989nccYUifUSq__fDJ8Ww.png)
   lambda expression with no arguments passed :point_up:

 ---

![image](https://miro.medium.com/max/865/1*GCAr46ui55zpiQnwdE5RLw.png)
   Lambda Expression with One Arguments Passed :point_up:
 ---

![image](https://miro.medium.com/max/853/1*MyRPIzNxXR94QegkxD7a5Q.png)
Lambda Expression with Two Arguments Passed :point_up:
 ---

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

 :dart: **Null Reference Template**
We can also call it the Java Optional Class.
* Advantages using the Java Optional class are,
1. No Null Pointer Exceptions at run-time.
2. Clean APIs can be developed.
3. Null checks are not required.

![image](https://miro.medium.com/max/864/1*LkwQwi3QD1H1skQhXuGWYA.png)
Java Optional Class Example :point_up:

  ---

 :dart: **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](https://miro.medium.com/max/875/1*_YHS9pKQQK5D_AJqtnSSIA.png)
Use of the New Date and Time API :point_up:
 ---

 :dart: **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](https://miro.medium.com/max/870/1*dHvTtGB5V2Z4DFMntzRMFg.png)
 ---

 :dart: **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](https://miro.medium.com/max/875/1*_LFTbDXOqiR3imK7MNdNeA.png)

 ---
 
 :dart: **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](https://miro.medium.com/max/865/1*ShUh_QTCtcUzkafqP1_e3A.png)

Credits-@Chinthaka Jayatilake .

 ---

 
 :dart: **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.
 ---

 :dart: **Java 8 Method References :**

:pushpin: 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.

:pushpin: **Syntax**

:paperclip: **Method reference to static method** :

 > ClassName::MethodName


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

> Integer::parseInt, Math::max


:paperclip: **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.

:paperclip: **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
 ---

:pushpin: **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](https://i0.wp.com/javaconceptoftheday.com/wp-content/uploads/2019/03/Java8MethodReferences.png?ssl=1)


 ---

 :dart: **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.
 ---


 :dart: **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.
 ---


:dart:  **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()
}
```
 ---


:dart:  **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.

 ---

:dart:  **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](https://i2.wp.com/javaconceptoftheday.com/wp-content/uploads/2019/03/Java8InterfaceChanges.png?ssl=1)

 ---

:dart:  **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()
}
```

 ---

:dart:  **Why Static Methods** :question:

**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.

 ---

 :dart: **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 :
:dart: **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.
 ---


:dart: **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.
 ---


:dart: **Constructors**
Interfaces can’t have constructors. Abstract classes can have any number of constructors.
 ---

:dart: **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.

 ---

:dart: **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.

 ---

 :dart: **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](https://i0.wp.com/javaconceptoftheday.com/wp-content/uploads/2019/04/InterfaceVsAbstractClassJava8.png?ssl=1)

```
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
}
``` 
 ---

**References:**

[Oracle Docs](https://www.oracle.com/java/technologies/java8.html)

[Other resources](https://www.javatpoint.com/java-8-features)
 
 --- 
 
Thank you for reading this Article. 😊

Please leave a :heart: 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](https://dev.to/the_unconventional_coder/java-concept-of-the-day-1jim) | 
| Java Generics |  [goto Article](https://dev.to/the_unconventional_coder/java-concept-of-the-day-oec)     |  
|Java Regex     | [goto Article](https://dev.to/the_unconventional_coder/lets-talk-about-regex-3npd)
| Java Streams Api | [goto Article](https://dev.to/the_unconventional_coder/lets-talk-about-streams-2bdf) |