Interview Questions & Answers

java-8

public class Test {

    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = new String("hello");

        s2 = s2.intern();
        System.out.println(s1 == s2);
    }
}

output:
true

Explanation: We know that the intern() method will return the String object reference from the string pool since we assign it back to s2 and now both s1 and s2 are having the same reference. It means that s1 and s2 references point to the same object.

public class Test {
    public void print(Integer i) {
        System.out.println("Integer");
    }

    public void print(int i) {
        System.out.println("int");
    }

    public void print(long i) {
        System.out.println("long");
    }

    public static void main(String args[]) {
        Test test = new Test();
        test.print(10);
    }
}

output:
int

Explanation: If Integer and long types are specified, a literal will match to int. So, the program prints int.

Using the name of the interface.

Example:-

interface Vehicle {
    static void blowHorn() {
        System.out.println("Blowing horn!!!");
    }
}
class Car implements Vehicle {
    public void print() {
        Vehicle.blowHorn();
    }
}

Using the super keyword along with the interface name.

Example:-

interface Vehicle {
    default void print() {
        System.out.println("I am a vehicle!");
    }
}
class Car implements Vehicle {
    public void print() {
        Vehicle.super.print();
    }
}

A Static Method is a Utility method or Helper method, which is associated with a class (or interface). It is not associated with any object.

We need Static Methods because of the following reasons:

  • We can keep Helper or Utility methods specific to an interface in the same interface rather than in a separate Utility class.
  • We do not need separate Utility Classes like Collections, Arrays, etc to keep Utility methods.
  • Clear separation of Responsibilities. That is we do not need one Utility class to keep all Utility methods of Collection API like Collections etc.
  • Easy to extend the API.
  • Easy to Maintain the API.

A default method is a method with an implementation – which can be found in an interface.
We can use a default method to add new functionality to an interface while maintaining backward compatibility with classes that are already implementing the interface:
public interface Vehicle {
    String getBrand();

    String speedUp();

    String slowDown();

    default String turnAlarmOn() {
        return "Turning the vehice alarm on.";
    }

    default String turnAlarmOff() {
        return "Turning the vehicle alarm off.";
    }
}


Usually, when a new abstract method is added to an interface, all implementing classes will break until they implement the new abstract method. In Java 8, this problem has been solved by the use of the default method.
For example, the Collection interface does not have a forEach method declaration. Thus, adding such a method would simply break the whole collections API.
Java 8 introduces the default method so that the Collection interface can have a default implementation of the forEach method without requiring the classes implementing this interface to implement the same.

  • Null checks are not required.
  • No more NullPointerException at run-time.
  • We can develop clean and neat APIs.
  • No more Boilerplate code

Optional is a container object which is used to contain not-null objects. An optional object is used to represent null with an absent value.
The Optional class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.
The purpose of the Optional class is to provide a type-level solution for representing optional values instead of using null references.