Review some thoughts in the basic knowledge of Java

1. The difference between overloading and rewriting

  • Overloading means that multiple methods with the same name in the same class perform different logical processing for different passed parameters.

  • Rewriting is the re-transformation of the parent class (or interface) method by the subclass, the appearance remains unchanged, and the internal logic can be changed.

2. Three major features of Java language: encapsulation, inheritance, polymorphism

Polymorphism: It means that the specific type pointed to by the reference variable in the program and the method call issued through the reference variable are not determined during programming, but determined during operation.

Two ways to achieve polymorphism:

  • Inheritance (multiple subclasses overwrite the same method)
  • Interface (implement the interface and override the same method in the interface)

3. Why is String immutable? What are the benefits of this design?

String is implemented by a final modified character array (after Java 9, the implementation of the String class uses byte arrays to store strings) and is therefore immutable.

benefit:

(1) Easy to implement String constant pool

Only when the string is immutable, the string pool is possible. The implementation of string pool can save a lot of heap space at runtime, because different string variables all point to the same string in the pool. But if the string is mutable, then String interning will not be implemented (Translator's Note: String interning means that only one string is stored for different strings, that is, multiple identical strings are not stored.), because of this. , If a variable changes its value, then the values ​​of other variables pointing to this value will also change.

(2) Avoid network security issues

If the string is variable, it will cause serious security problems. For example, the user name and password of the database are passed in in the form of strings to obtain the database connection, or in socket programming, the host name and port are passed in in the form of strings. Because the string is immutable, its value cannot be changed. Otherwise, hackers can exploit the loopholes and change the value of the object pointed to by the string, causing security vulnerabilities.

(3) Make multithread safe

Because strings are immutable, they are multi-thread safe, and the same string instance can be shared by multiple threads. This eliminates the need for synchronization due to thread safety issues. The string itself is thread-safe.

(4) Avoid local security issues

The class loader uses strings, and immutability provides safety so that the correct class is loaded. For example, if you want to load the java.sql.Connection class, and this value is changed to myhacked.Connection, it will cause unknowable damage to your database.

(5) Speed ​​up string processing

Because the string is immutable, the hashcode is cached when it is created and does not need to be recalculated. This makes the string very suitable as a key in the Map, and the processing speed of the string is faster than other key objects. This is that the keys in HashMap often use strings. The immutability of String guarantees the immutability of the hash value.

4. What is the difference between access and abstract class?

  1. The access method is public abstract by default. All methods before Java 8 cannot be implemented in the interface (Java 8 starts with the access method can have a default implementation), but abstract classes can have non-abstract methods.
  2. In addition to static final variables, there can be no other variables in the interface, but not in the abstract class.
  3. One class can implement multiple interfaces, but only one abstract class can be implemented. The interface itself can extend multiple interfaces through the extends keyword.
  4. The default modifier of the access method is public, and the abstract method can have public, protected, and default modifiers (the abstract method is to be rewritten so it cannot be modified with the private keyword!).
  5. From the design level, abstraction is the abstraction of classes, it is a template design, and the connection is the abstraction of the behavior, and it is a norm of behavior. An interface is just a higher level abstraction of the properties and behaviors of a class of things. It is closed for modification and open for extension (different implementations). The interface is a manifestation of the principle of opening and closing.
这里延伸一道题吧:
Java接口的修饰符可以为()
A private B protected C final D abstract

解析:
答案:D
解析:接口很重要,为了说明情况,这里稍微啰嗦点:

(1)接口用于描述系统对外提供的所有服务,因此接口中的成员常量和方法都必须是公开(public)类型的,确保外部使用者能访问它们;

(2)接口仅仅描述系统能做什么,但不指明如何去做,所以接口中的方法都是抽象(abstract)方法;

(3)接口不涉及和任何具体实例相关的细节,因此接口没有构造方法,不能被实例化,没有实例变量,只有静态(static)变量;

(4)接口的中的变量是所有实现类共有的,既然共有,肯定是不变的东西,因为变化的东西也不能够算共有。所以变量是不可变(final)类型,也就是常量了。

(5) 接口中不可以定义变量?如果接口可以定义变量,但是接口中的方法又都是抽象的,在接口中无法通过行为来修改属性。有的人会说了,没有关系,可以通过 实现接口的对象的行为来修改接口中的属性。这当然没有问题,但是考虑这样的情况。如果接口 A 中有一个public 访问权限的静态变量 a。按照 Java 的语义,我们可以不通过实现接口的对象来访问变量 a,通过 A.a = xxx; 就可以改变接口中的变量 a 的值了。正如抽象类中是可以这样做的,那么实现接口 A 的所有对象也都会自动拥有这一改变后的 a 的值了,也就是说一个地方改变了 a,所有这些对象中 a 的值也都跟着变了。这和抽象类有什么区别呢,怎么体现接口更高的抽象级别呢,怎么体现接口提供的统一的协议呢,那还要接口这种抽象来做什么呢?所以接口中 不能出现变量,如果有变量,就和接口提供的统一的抽象这种思想是抵触的。所以接口中的属性必然是常量,只能读不能改,这样才能为实现接口的对象提供一个统 一的属性。
通俗的讲,你认为是要变化的东西,就放在你自己的实现中,不能放在接口中去,接口只是对一类事物的属性和行为更高层次的抽象。对修改关闭,对扩展(不同的实现 implements)开放,接口是对开闭原则的一种体现。

所以:
 **接口的方法默认是public abstract;
 接口中不可以定义变量即只能定义常量(加上final修饰就会变成常量)。所以接口的属性默认是public static final 常量,且必须赋初值。**

注意:final和abstract不能同时出现。

Remarks:

  1. In JDK8, the interface can also be defined with a static method, which can be called directly with the name of the interface. Implementation classes and implementations cannot be called. If two interfaces are implemented at the same time, and a default method is defined in the interface, it must be rewritten, otherwise an error will be reported.
  2. How to have a default implementation in the interface:
    Insert picture description here
    **Benefits: **There is a default implementation in the interface, which can make the interface expand without breaking the code of the implementation class related to the interface.
  3. The jdk9 interface is allowed to define private methods.

Guess you like

Origin blog.csdn.net/weixin_42118981/article/details/114108331