It is high - time to learn Java 8 |
Why Java 8 ?
Java 8 is a major feature release for Java programming language. This blog has an introductory question set that explains the Interview Questions asked on Java 8 and their usage in a simple way.
Recently many of the companies are moving on from Java 1.7 to Java 8 which is leading a Java developer to understand atleast the basics of this Java version.
So, do not miss going through the question set if you are appearing for Java interview.
Why Java 8 ?
Java 8 is a major feature release for Java programming language. This blog has an introductory question set that explains the Interview Questions asked on Java 8 and their usage in a simple way.
Recently many of the companies are moving on from Java 1.7 to Java 8 which is leading a Java developer to understand atleast the basics of this Java version.
So, do not miss going through the question set if you are appearing for Java interview.
1. GENERAL QUESTIONS :
Q1. What are the Features of Java8 ?- Functional Interfaces
- Lambda Expression
- Method References
- Default/Static Methods in Interface
- Static Methods in Interface
- Stream API
- Parallel array sorting
- Optional Class
- DateTime API
- Base64 encode/decode
- Collectors class
- IO enhancements
- Concurrency enhancements
- JDBC enhancements
- Nashorn JS engine
- forEach() Method
- StringJoiner class
- Type Inference
- Parameter Reflection
- etc.
2. FUNCTIONAL INTERFACES :
Q2a. What are Functional Interfaces?
- Also known as "Single Abstract Method Interfaces" (SAM Interfaces)
- It is an interface that contains only 1 abstract method.
- Further, can have any no. of default, static methods.
- It can declare methods of Object class.
@FunctionalInterface // Not Mandatory
interface Drawable{
public void draw(String clr); // #1 Abstract method
int hashCode(); // #2 Object class methods
String toString();
}
public class B1_LambdaExpression {
public static void main(String[] args) {
int width = 10;
Drawable d = new Drawable(){
public void draw(String clr){
System.out.println("1. Width:"+width+ "\tClr:"+clr);
}
};
d.draw("Red");
}
}
Output:
1. Width:8 Clr:Red2. Width:8 Clr:Blue
- Yes, only if that interface does not have any abstract method.
{{{CODE HERE}}}
Q2c. Can a functional interface extend a non-functional interface ?
- Yes.
3. LAMBDA EXPRESSIONS :
Q3a. What is Lambda Expression ?
2. Width:8 Clr:Blue
- It is an alternate for "Anonymous inner class".
- It provides clear way to represent 1-method interface using an expression.
- It is very useful in Collection library to iterate,filter,extract data from collection.
- It is treated as a function, so compiler does NOT create .class file.
Q3b. What are advantages of Lambda Expression ?
- To provide implementation of Functional Interface.
- Less coding.
Q3c. Show simple code with & without lambda expression.
@FunctionalInterface // Not Mandatory
interface Drawable{
public void draw(String clr);
}
public class B1_LambdaExpression {
public static void main(String[] args) {
int width = 10;
// Java7
Drawable d = new Drawable(){
public void draw(String clr){
System.out.println("1. Width:"+width+ "\tClr:"+clr);
}
};
d.draw("Red");
// Java8
Drawable d2 = clr -> {
System.out.println("2. Width:"+width+ "\tClr:"+clr);
};
d2.draw("Blue");
}
}
Output:
1. Width:8 Clr:Red2. Width:8 Clr:Blue
Q2d. Use ForEach loop with Lambda expression.
interface Drawable{
public void draw(String clr);
}
public class B1_LambdaExpression {
public static void main(String[] args) {
ArrayList list = new ArrayList(); // For Array or Set
list.add("aaa");
list.add("ccc");
list.add("bbb");
list.forEach(n->System.out.println(n));
Map map = new HashMap(); // For Map
map.put("aaa","AAA");
map.put("ccc","CCC");
map.put("bbb","BBB");
map.forEach((k,v) -> System.out.println("Key : "+k +"\tVal : "+v ));
}
}
Output:
aaa
ccc
bbb
Key : aaa Val : AAA
Key : ccc Val : CCC
Key : bbb Val : BBB
public void draw(String clr);
}
public class B1_LambdaExpression {
public static void main(String[] args) {
ArrayList
list.add("ccc");
list.add("bbb");
list.forEach(n->System.out.println(n));
Map
map.put("aaa","AAA");
map.put("ccc","CCC");
map.put("bbb","BBB");
map.forEach((k,v) -> System.out.println("Key : "+k +"\tVal : "+v ));
}
}
Output:
ccc
bbb
Key : aaa Val : AAA
Key : ccc Val : CCC
Key : bbb Val : BBB
public static void main(String[] args) {
Runnable r1 = new Thread(){ // Java 7
public void run(){
System.out.println("t1 runs...");
}
};
r1.run();
Runnable r2 = ()->{ // Java 8
System.out.println("t2 running...");
};
r2.run();
}
Output:
t1 runs...
t2 running...
4. METHOD REFERENCES :
- It is used to refer method of Functional Interface.
- It is compact and easy form of lambda expr.
- It is described using
::
symbol.
Q4b. What are types of Method References?
- Reference to
- Static method
- Instance method of an object
- Instance method of arbitrary object of a type
- Constructors using new operator (TreeSet::new)
Q4c. Explain Reference to Static method.
- Syntax :
ContainerClass::staticMethodName
5. DEFAULT/STATIC METHODS IN INTERFACE :
Q5a. What are Default-methods?
- Java 8 provides a facility to create default methods inside the interface.
- Methods should be tagged default.
- Those are non-abstract methods.
- You can define static methods inside the interface.
- They are used to define utility-methods.
Q5c. Give an example of Default & Static Method.
{{{CODE}}}
Q5d. What is difference between Abstract-class & Java8 Interface?
- Abstract-class & Interface are almost similar (with default and static methods in Interface), except that you can create Constructor in Abstract-class but not in interface.
6. forEach() METHOD :
Q6a. What is forEach() method?- It is used to iterate the elements.
- It is default method defined in <Iterable> & <Stream> interface.
- Signature in Iterable Interface :default void forEach(Consumer<super T>action)
- {{{CODE}}}
Q6b. What is Stream forEach() method?
- It is used to iterate elements in the order specified by the stream.
- Signature:
void forEachOrdered(Consumer<? super T> action) - {{{CODE}}}
To be continued...
No comments:
Post a Comment