Java Programs and Spring Boot
Examples
WAP to implement the following new features in Java
import [Link];
@FunctionalInterface
interface StringChecker {
boolean check(String s);
static void printInfo() {
[Link]("Static Method in Functional Interface");
}
default void printDefault() {
[Link]("Default Method in Functional Interface");
}
}
public class JavaFeaturesDemo {
public static void main(String[] args) {
// (a) Functional Interface + (b) Lambda Expression
StringChecker isEmpty = (s) -> [Link]();
[Link]("Is empty: " + [Link](""));
// (c) Method Reference
Predicate<String> isEmptyRef = String::isEmpty;
[Link]("Using Method Reference: " + [Link](""));
// (d) Default and Static Method in Interface
[Link]();
[Link]();
// (e) Inner Class
Outer outer = new Outer();
[Link] inner = [Link] Inner();
[Link]();
}
}
class Outer {
class Inner {
void display() {
[Link]("This is an Inner Class");
}
}
}
Different types of annotations
import [Link].*;
import [Link].*;
import [Link];
@Retention([Link])
@Target([Link])
@interface MyAnnotation {
String value() default "Default";
}
class Demo {
@Deprecated
public void oldMethod() {
[Link]("Deprecated method");
}
@SuppressWarnings("unchecked")
public void warningMethod() {
List rawList = [Link]("Java", "Annotations");
List<String> stringList = rawList;
[Link](stringList);
}
@MyAnnotation(value = "Custom Annotation")
public void customAnnotatedMethod() {
[Link]("Custom annotated method");
}
}
public class AnnotationExample {
public static void main(String[] args) throws Exception {
Demo demo = new Demo();
[Link]();
[Link]();
[Link]();
Method method = [Link]("customAnnotatedMethod");
if ([Link]([Link])) {
MyAnnotation annotation = [Link]([Link]);
[Link]("Annotation Value: " + [Link]());
}
}
}