Java based
How does Java work?
The Java source code developed is compiled by Javac into platform-independent bytecode files (classes), which are then interpreted into the corresponding machine code by the JVM’s interpreter
The “compile once, run anywhere” understanding
Java is a cross-platform language. Java is a cross-platform language. Java is a cross-platform language. The key and premise for running everywhere is the JVM, which has a JVM operating system embedded wherever the JVM can be run, enabling Java to provide virtual mechanisms on a variety of platforms, thus achieving the effect of running everywhere
JDK, JRE, JVM differences
JDK | Java Development Kit |
JRE | Java runtime environment, containing the JVM and Java class libraries |
JVM | Virtual machine, a runtime environment that runs Java bytecode |
What are encapsulation, inheritance, abstraction?
encapsulation | Hide the attributes and implementation details of the object, and only provide public access — security, reusability |
inheritance | Is -a relationship in which subclasses accept common attributes and methods and add unique attributes and methods |
abstract | Common method names and classes that are implemented by subclasses |
Inheritance Considerations
1. A subclass can only inherit non-private members (member methods, member variables) from its parent class.
2. Subclasses cannot inherit the constructor of their parent class, but can access the constructor of their parent class through the super keyword
Equals and ==
= = | It compares the heap memory address of variables in memory to determine whether the address of two objects is the same, that is, whether they point to the same object |
equals | Equals equals equals equals equals equals equals equals equals equals equals equals equals |
Some conventions for HashCode and Equals
A: Equals equals, hashCode must be equal
B: Rewrite hashCode and also rewrite equals
C: hashCode needs to be consistent, and the hash value returned by state changes still needs to be consistent
How do you understand polymorphism? Benefits? Why polymorphism?
Polymorphism: A reference from a parent class to a subclass
Benefits: The functions of subclasses can be called by methods or reference variables of the parent class
Why: Reusability, High cohesion, low coupling, scalability
The difference between super and this
this | Represents an object reference to this class |
super | Refers to the reference of the parent class, and identifies the storage space of the parent class |
Code block execution order
Static code block (executed once) -> Construct code block (executed before each constructor execution) -> constructor
Common class initialization order
Static properties (variables, methods) -> Static code blocks -> Member properties -> Construct code blocks -> Constructors
The initialization order of inherited subclasses
Superclass Static Properties -> Superclass Static Code Block -> Subclass Static Properties -> Subclass Static Code Block -> Superclass Member Variable -> Superclass Constructor code block -> Subclass Member Variable -> Subclass Constructor code block -> Subclass Constructor
The difference between final, finally and Finalize
final | Modifies variables: Variables of primitive data types cannot be reassigned, and variables of reference types cannot point to other objects. Modifies methods: Methods cannot be overridden, that is, the method does not need to be extended |
finally | A finally block is always executed, usually to reclaim a resource (database shutdown, IO shutdown, network shutdown, etc.) |
finalize | Ensure that an object completes a specific resource collection before being garbage collected |
Java exception class correlation
Error | Unchecked exceptions and system-level errors cannot be recovered or prevented, such as system crashes, VM errors, and insufficient memory space |
Exception | There are RuntimeException (unchecked) and IOException (to be checked), program-level errors that can be recovered and prevented. Creating a class that inherits Exception is to be checked |
throw | When used in a method body, the exception object name can be thrown to any Throwable, which is processed by a statement or throws throws in the method body |
throws | Used after the method name, followed by the exception name, to indicate that exceptions may be thrown that do not necessarily occur and are handled by the method caller |
The difference between interfaces and abstract classes
interface | 3, public only, no constructor, can’t run without main function 4,Main functions:The API defines and implements the purpose of separation |
An abstract class | Public, protected, private; constructor; main;Main functions:Code reuse |