1. The difference between & and &&

& : bitwise and, whether the left side is true or false, the right side will be computed without short-circuit;

&& : logical and. If the left side is true, the right side participates in the operation. If the left side is false, the right side does not participate in the operation and has the function of short-circuit.

2. The difference between overloading and overwriting

Overloading: A class with the same method name but different argument lists, regardless of the method modifiers or return value types

Rewrite: Need to follow the “two same two small one” principle.

  • Identical: The method overridden by a subclass must have the same name and argument list as the method overridden by its parent class

  • Two minor: the return value type of the subclass is less than or equal to the return value type of the parent class, and the exception thrown by the subclass is less than or equal to the exception thrown by the parent class.

  • One: methods overridden by subclasses use more access than methods overridden by their parent class.

Pay attention toA subclass cannot override a method declared private by its parent class

The difference between equals and equals

== can compare both base and reference types. Values are compared for primitive types, and memory addresses are compared for reference types.

Equals, which belongs to the java.lang.Object class and defaults to == if the method has not been overridden; Normally, overriding equals compares whether the corresponding properties in the class are all equal.

4. Differences between final, finally and Finalize

  • final: used to declare a property, method, and class, indicating that the property is immutable, the method is not overridden, and the class it modifies is not inheritable.
  • finally: Part of the exception handling statement structure that always executes.
  • finalizeA method of the: Object class that can be overridden by the garbage collector, which calls the method of the reclaimed Object when it is executed

Interfaces and abstract classes

The same:

  • Neither interfaces nor abstract classes can be instantiated; they are at the top of the inheritance tree and are used to be implemented and inherited by other classes
  • You can use abstract classes and interface types as reference types
  • Both interfaces and abstract classes contain abstract methods
  • A class that inherits an abstract class or implements an interface must implement all of its abstract methods. Otherwise, the class still needs to be declared as abstract
The difference between:
  • A class can implement multiple interfaces, and a class can inherit only one abstract class.
  • . Constructors cannot be defined in interfaces, but can be defined in abstract classes. The constructor of an abstract class is not used to create an object, but to let subclass calls complete the initialization of the abstract class.
  • All the methods in the interface are abstract methods. Abstract classes can have abstract methods and concrete methods.
  • Members in an interface are all public. Members in an abstract class can be private, default, protected, or public.
  • Member variables defined in interfaces are actually constants. Member variables can be defined in abstract classes.
  • Interfaces cannot have static methods. Abstract classes can contain static methods. (Note: static methods can be used in interfaces after JDK1.8)

6. Difference between Error and exception

The same:

The parent of the Error and Exception classes are Throwable classes

The difference between:

The Error class generally refers to serious problems that cannot be solved by the Java virtual machine, such as system crashes, virtual machine errors, insufficient memory, method call stack overflow, etc. The Exception class represents other general problems caused by programming errors or accidental external factors, exceptions that a program can handle, catch, and possibly recover.

Throw throws

Throw:

  • A throw statement is used in a method body to indicate that an exception is thrown and is handled by a statement in the method body.
  • A throw is an action that specifically throws an exception, so it throws an exception instance, and executing a throw must throw some kind of exception.

Throws:

  • The throws statement is used after a method declaration to indicate that if an exception is thrown, the method caller handles the exception.
  • Throws throws throws throws throws of a certain type, letting its users know the type of exception to catch.
  • Throws represents a possibility of an exception that is not necessarily expected.

8, StringBuilder, StringBuffer

  • StringModified by final, is an immutable sequence of characters
  • StringBufferMutable character sequences, which are inefficient but thread-safe
  • StringBuilderMutable character sequences, which are efficient but not thread-safe

ArrayList and Linkedlist

Both are not thread-safe, but are more efficient than Vector

  • ArrayListThe bottom layer stores the data as an array and randomly accesses the elements of the collection faster than LinkedList (because LinkedList moves the pointer).
  • LinkedListThe interior stores the data in the collection as a linked list, which randomly accesses the elements of the collection but is slower to add and delete than ArrayList (because ArrayList moves the data).

Comparable and Comparator

Comparable and Comparator are both used to compare and sort elements in a collection. Comparable is a comparison of methods that are defined inside the collection. Comparator is a comparison of sorts that are implemented outside the collection. You need to define methods for the Comparator interface outside the collection or implement methods for the Comparable interface within the collection.

The difference between Equals and HashCode

  • If two objectsequalsEqual, the Java runtime environment will consider themhashcodeIt must be equal.
  • If two objectsequalsNot equal to theirshashcodeIt could be equal.
  • If two objectshashcodeEqual, theirsequalsNot necessarily equal.
  • If two objectshashcodeNot equal to theirsequalsNot necessarily equal.