1. Similarities and differences between interfaces and abstract classes

  • The same:

    • Anyway, you can’t create objects
    • Anyway, you can define abstract methods, which must be overridden in a subclass
  • Different:

    • The static key word is different abstract-interface
    • Anyway, an abstract method can involve both abstract and generic methods
    • Anyway, all the methods in the interface are abstract methods
    • Anyway, methods of an abstract class can have arbitrary privileges, while methods in an interface can only be public
    • Confer Abstract classes can only be inherited singly, and interfaces can be implemented concurrently

2. The difference between overload and overwrite

  • Rewritten rules

Subclass and superclass, subclass overrides the methods of the parent class

The method name and argument list must be exactly the same as the parent class

The return value type is either the same, or the return value type of the subclass method is a subclass of the return value type of the parent method!

Access modifiers are either the same or subclass access modifiers are larger than the parent class!

Method, or the same exception. Either the subclass method throws a smaller or identical exception than the overridden method of the parent class!

Rules for overloading (two identical and different)

1) In the same class

2) The method name is the same

3) Different parameter lists (number, list, type)

4) Independent of the return value

3. StringBufferStringBuilderString difference

String String constants are immutable new space is created when String concatenation is used

StringBuffer Variable thread-safe string concatenation is appended directly to the string

String variable variable non-thread-safe string concatenation appends directly to the string

1.StringBuilder Performs StringBuffer more efficiently than String.

2.String is a constant and immutable, so a new object is created for each += assignment,

Both StringBuffer and StringBuilder are mutable, and when concatenating strings, append them to the original, so they perform better than strings.

StringBuffer is thread-safe and StringBuilder is thread-safe, so StringBuilder is more efficient than StringBuffer.

3. StringBuilder is used to concatenate strings with a large amount of data.

4. Bubble sort

Utility class version:

5. Select sort

6. Singleton design pattern

A singleton is a class that returns only one instance. Characteristics of singletons:

  • 1. Privatizing constructors

  • 2. Private static global variables

  • Public static methods

LanHanShi:

Thread-safe slob:

The hungry type:

7. Common exception types

  • NullPointerException NullPointerException

  • ClassCastException type cast exception

  • IllegalArgumentException Invalid argument passing exception

  • ArithmeticException arithmetic ArithmeticException

  • Abnormal IndexOutOfBoundsException subscript crossing the line

  • NumberFormatException The number format is abnormal

  • ClassNotFindException Load request exception

Throws Throws

  • Queue position: Throws inside a method and Throws after the method name

  • Throws an exception and Throws an exception. Throws Throws an exception

  • Throws an exception at a time. Throws multiple exceptions

9. The role of Final, Finally, Finalize keywords

  • Final is used to declare properties, methods, and classes, indicating that properties are immutable, methods are not overridden, and classes are not inherited, respectively.

  • Finally is part of the exception handling statement structure and means always execute. It is always executed, regardless of exceptions, unless the virtual machine is stopped. System.exit(1);

  • Finalize is a method of Object class. When garbage collector executes, finalize will call the Object to be reclaimed, which can realize resource collection and release resources, such as closing files, etc. The JVM does not guarantee that this method will always be called.

10. The role of the Final keyword

  • Queue decorates classes :(final class) cannot be inherited by subclasses
  • A modified method :(final method) cannot be overridden by subclasses
  • Undefined variables :(constants) once declared, they can’t be changed again