Advanced Java Flashcards

Java 8 features

1
Q

What are the two new packages in Java 8?

A

java. util.function

java. util.stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Functional Interface?

A

The interface having only one abstract method is called functional interface. It represents abstract concepts such as functions, actions or predicates.

For eg. Predicate, Consumer, Function, Supplier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the Predicate interface.

A

It has a method named test which takes one argument and evaluates it to return boolean value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the Consumer interface.

A

It accepts single argument with no return value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an Anonymous class?

A

Anonymous class is an inner class with no name.

They are useful when we have to modify the implementation of a method only once of some class/interface on the fly without actually having to extend the class/implementing the interface.

For eg.

@FunctionalInterface
public interface Foo {
public int calc (int a, int b);
}

If we want to provide a different implementation for this method but do not want to implement the Foo interface, we can have an anonymous inner class like this:

Foo foo=new Foo ( ) {
@Override
public int calc( int a, int b) {
return a+b;
}
};

And then call it like this:

System.out.println ( foo.calc ( 10, 20 ) );

If we want the function to perform the subtraction, we can modify the behaviour.

With anonymous inner class, we can avoid adding new .java files to project. This comes in handy during creation of various event listeners. If we do not use anonymous inner classes, a separate class that implements the event listener interface will be required for each event. This approach will take lot of coding for a simple operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain lambda expression.

A

If we are implementing a functional interface using an anonymous class, the code might seem lengthy to perform a single operation. With lambda expression, we can minimize the amount of code and treat the functionality as method argument.

For eg.

@FunctionalInterface
public interface Foo {
public int calc (int a, int b);
}

Implementation of this using anonymous inner class:

Foo foo=new Foo ( ) {
@Override
public int calc ( int a, int b) {
return a+b;
}
};

Implementation using lambda expression:
Foo add = (a,b) -> a+b;
Foo diff = (a,b) -> a-b;

As this is a single expression (as opposed to block lambda), we can omit return statement.

Even when the method does not take any argument, lambda expression must specify the argument parenthesis.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to create a Lambda expression without arguments?

A

Anonymous class:

Runnable r1 = new Runnable ( ) {
			@Override
			public void run ( ) {
				System.out.println ("run 1");
			}
		};

Lambda expression:

Runnable r2= () -> System.out.println(“run 2”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain method reference.

A

With lambda expression, we can take any method (static or instance) or even a constructor and convert it into a lambda. This is called method reference and is denoted with symbol ‘::’.

Consumer print = ( str ) -> System.out.println (str);
print.accept( “Hello” );

With method reference:
Consumer print =System.out : : println;
print.accept( “Hello” );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the Comparator in Java 8.

A

We can use lambda expression to create a Comparato, such as

Comparator byAge =
(Player p1, Player p2) -> Integer.compare(p1.getAge( ), p2.getAge( ));

We can also use Comparator.comparing( ) method. It takes a property for comparing items and returns a Comparator instance:

Comparator byAge =
Comparator.comparing(Player :: getAge);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the features of Java 8?

A
  1. Functional Interfaces and Lambda expressions
  2. Interfaces with default and static methods
  3. Java Stream API
  4. Java Date Time API
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a default method in interface?

A

Java 8 provides interfaces containing methods with body, labeled as default methods, also known as Defender methods or Virtual extension methods. This feature was introduced in Java 8 to provide backward compatibility so that existing interfaces can use the lambda expressions without getting broken.

If an interface is implemented by multiple classes, it becomes mandatory for all the classes to override all the methods in interface. With default methods, classes do not have to provide the behaviour.

For eg.
public default void logging ( ) {
System.out.println ( “IF1” );
}

If a class implements two interfaces containing exact same default methods, the class must provide its own implementation of the method to get rid of compile-time error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain static methods in interface.

A

Apart from default methods, Java 8 also allows us to define and implement static methods in interface. They make it possible to group related utility methods, without having to create separate utility classes that are simply placeholders for static methods.

Since static methods don’t belong to an object, they will not be part of API of classes implementing the interface and they will be called by using the interface name preceding the method name. Due to this, static methods in interfaces cannot be overridden in implementing classes. Trying to override the static method in implementing class with @Override annotation will result in compile-time error.

This static method in an interface can be invoked within other static and default methods.

For eg.
public static void message( ) {
		System.out.println("inside static method");
}
public default void logging ( ) {
		System.out.println ( "IF1" );
message( );
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly