These simple and stupid mistakes that I made during an interview Part 1

Introduction

All right, I failed another interview as usual. I always made very simple errors, like yesterday, I had an interv iew with an india company. Their interview questions are more focused on basic knowledge of candidate. I record servral questions which I failed to answer. Here I reviewed them and write the answers out.

1. Functional Interface

The first question is about one the feature of Java 8, functional interface. To be honest, I just have a very ambiguous overview about it.
I write down the answer below.

1.1 Introduction

Functional interface is defined by only have single abstract method, so it is also called SAM interface, but it accepts multiple default and static methods.**

@FunctionalInterface
public interface MyInterface {
    public int add(int a, int b);
}
//Usage
public class App {
    public static void main(String[] args) throws Exception {
        MyInterface myInterface=(x,y)->x+y;
    }
}

You might notice that there is no abstract keyword used before add() method, that is because, by default, a method declared inside a functional interface is abstract.

1.2 Declaration

You might have noticed that the first line of above snippet is @FunctionalInterface annotation. Yes, it’s built-in Java annotation. You must have it to build your own functional interface.
Apart from that, Java also offers some predefined functional interfaces, like Runnable, Callable, Function, Consumer etc.

1.3 Benefits

The main reason why we need functional interfaces is that we can use them in lambda expressions and method references. This way, we reduce boilerplate code.

Before, we would have to use an Anonymous class, like below. But functional interfaces make the code more straightforward to read. For example, an anonymous class is created like this:

public class AnonymousClassExample {
    public static void main(String[] args) {
        Thread thread = new Thread(){
            public void run(){
                System.out.println("Understanding what is an anonymous class");
            }
        };
        thread.start();
    }
}

Now, with Runnable interface, it looks like this.

public class AnonymousClassExample {

    public static void main(String[] args) {

        Runnable runnable = () -> {
            System.out.println("Understanding functional interfaces");
        };
        runnable.run();
    }
}

It’s shorter and easier to read, right?

2. Default method

Another mistake I made is mixed Static method with another feature of Java 8, Default method.
Before Java 8, interface can only contain abstract method. Since Java 8, Static method and Default method are introduced in interface. It hugely expands the functions of interface without breaking the heritance relationship of its derived class.

There’s an interface

public interface MyInterface {
    public int add(int a, int b);
}

User asks to add another mothod into MyInterface, like divid(int, int). With Static method and default method, I can accomplish it without defining a divid(int, int) abstract method.

  • The example of Static method
@FunctionalInterface
public interface MyInterface {
    public int add(int a, int b);
    static double divid(int a, int b){
        System.out.println("I am static");
    }
}
//Usage
public class App {
    public static void main(String[] args) throws Exception {
        System.out.println(MyInterface.divid(3, 3));
    }
}
//result
PS D:\code\FuntionalInterface>  d:; cd 'd:\code\FuntionalInterface'; & 'C:\Program Files\Java\jdk-17.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'D:\code\FuntionalInterface\bin' 'App'
1
  • The example of Default method
@FunctionalInterface
public interface MyInterface {
    public String des="abc";
    public int add(int a, int b);
    default public int divid(int a, int b){
        return a*b;
    }
}
//Usage
public class App {
    public static void main(String[] args) throws Exception {
    	MyInterface myInterface=(x,y)->x+y;
        System.out.println(myInterface.add(1, 1));
    }
}
//result
PS D:\code\FuntionalInterface>  d:; cd 'd:\code\FuntionalInterface'; & 'C:\Program Files\Java\jdk-17.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'D:\code\FuntionalInterface\bin' 'App'
2
  • Difference

Default methods:
A default method is a method declared with the default keyword in a functional interface.
Default methods have a default implementation, which can be overridden by implementations of the functional interface in concrete classes.
Default methods are used to provide a default behavior for the functional interface, but can be customized by concrete classes if needed.
Static methods:
A static method is a method declared with the static keyword in a functional interface.
Static methods are not associated with a particular instance of the functional interface and are called using the name of the functional interface, not an instance of the interface.
Static methods are used to provide utility methods that are associated with the functional interface as a whole, rather than with individual instances of the interface.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Minimal Mistakes个人主页是一个用于个人或专业用途的响应式网页模板。它提供了一种简单、干净和直观的设计风格,适用于各种不同的需求。 首先,Minimal Mistakes的布局简洁明了,没有多余的装饰或复杂的设计元素,使得页面易于阅读和导航。页面上的内容以有序的方式呈现,帮助用户更快地找到需要的信息。 其次,Minimal Mistakes提供了多种可自定义的选项,以满足不同用户的需求。用户可以轻松地更改网页的颜色、背景、字体和其他样式,以适应自己的品牌或个人偏好。此外,它还可以集成各种插件和功能,例如博客、作品集、社交媒体链接等,以展示个人或专业成果。 此外,Minimal Mistakes的个人主页还具有高度的可扩展性。用户可以根据需要添加或删除页面和部分。此外,该模板还兼容多种浏览器和移动设备,确保页面在不同平台上的显示和功能都能良好运作。 最后,Minimal Mistakes采用了响应式设计,意味着页面将根据用户的设备大小和屏幕分辨率自动适应。这使得用户可以在不同设备上无缝浏览和访问个人主页,无需担心兼容性问题。 总之,Minimal Mistakes个人主页是一个简洁、灵活和易于定制的网页模板,适用于个人或专业用途。它提供了出色的可读性和导航,同时具有高度的可扩展性和响应性。无论是展示个人作品还是个人品牌,Minimal Mistakes都是一个优秀的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值