Location>code7788 >text

Challenging Java Interview Questions Review Day 1, Persistence is Victory

Popularity:850 ℃/2024-10-26 22:27:30

The Difference Between Object-Oriented and Process-Oriented
Process-oriented:

Step-by-step analysis: breaking down the problem into a series of steps.
Function implementation: Step-by-step implementation of these steps with functions.
Call Execution: Call these functions when needed.
High performance: Suitable for applications with high performance requirements, such as microcontroller and embedded development.
Object-oriented:

Object Decomposition: Decompose the problem into multiple objects.
Behavioral description: the object describes the behavior of things during the problem solving process.
Features: encapsulation, inheritance, polymorphism.
Advantages: easy to maintain, reuse and extend, can build low-coupling system.
Performance: low performance compared to process-oriented.
Naming rules for identifiers
Identifier:

Definition: custom elements of a program, such as class names, method names, variable names, etc.
Naming rules (hard requirement):
Composition: can contain letters, numbers, $, _.
Restrictions: cannot start with a number.
Avoid: No keywords should be used.
Naming conventions (not mandatory):

Class name: capitalize the first letter, and capitalize the first letter of each subsequent word (big hump style).
Variable name: first letter lowercase, first letter of each subsequent word capitalized (small hump style).
Method name: same specification as variable name.
Automatic packing and unpacking
Boxing:
Definition: automatically converts a basic data type to the corresponding wrapper type.
Example: Convert int to Integer.
Method: realized by (int).
Unpacking:
Definition: automatically converts a wrapper type back to the corresponding basic data type.
Example: Convert Integer to int.
method: realized by ().
Pre-Java SE5 crating:
The new keyword needs to be used explicitly to create an instance of the wrapper type.
Example: Integer i = new Integer(10);
Java SE5 and later crates:
Provides an autoboxing feature that simplifies the creation of wrapper types.
Example: Integer i = 10; (auto call (int))
Difference between overloading and rewriting
Override:

Parent-child class relationship: occurs only between a subclass and its parent.
Method signatures are consistent: the method name, parameter list, and return type (except for subclasses where the return type of the subclassed method is a subclass of the parent's return type) must be the same as the method being overridden.
Access rights: access modifiers for subclass methods cannot be stricter than those for parent methods.
Exception restriction: rewritten methods cannot throw new or broader checked exceptions.
Overload:

Polymorphic manifestation: a manifestation of polymorphism in a class.
Parameter list differences: requires that the parameter list of a method with the same name must be different (either in parameter type, number, or order).
Return type: there is no restriction on the return value type, it can be the same or different, the judgment of overloading is based on the parameter list rather than the return type.
Difference between equals and ==
== operator:

Address Comparison: Compare whether two objects have the same address in memory.
Object reference: determine whether two references point to the same object.
Type requirement: operands must be of the same type or have an inheritance relationship.
Numeric Comparison: For basic data types, if the values are equal, the comparison result is true.
Pointer operations: essentially comparing pointer addresses.
The equals method:

Content Comparison: compares the content of two objects to see if they are equal.
Inherit from Object: All classes inherit from class and apply to all objects.
Default behavior: If the equals method is not overridden, it behaves like ==.
Null Pointer Safety: Avoids NullPointerException when comparing using equals.
Best Practice: It is recommended to use the equals method when comparing objects, especially in the Ali code specification.
Summary:

The equals method should be used when comparing objects for equality.
When comparing constants, write the constants first to avoid a possible NullPointerException.
It is recommended to use the Ali code specification and use the Ali plugin to check and replace inappropriate == usage.
The Role of Hashcode
Java collections are categorized into List and Set:

List: ordered, duplicates allowed.
Set: unordered, no duplicates allowed.
Set determines element existence:

Use the equals method, but it is inefficient with a large number of elements.
Hashing algorithms improve efficiency:
Divide the collection into multiple storage areas.
Each object determines the storage area by calculating a hash code through the hashCode method.
hashCode method:

Calculates a value based on the object's memory address.
When a new element is added to a collection, the storage location is first located by the hashCode method.
If the position has no element, store it directly; if the position has an element, use the equals method to compare.
If they are the same, they are not stored, and if they are different, they look for another location.
Reduce the number of equals method calls to improve performance.
Differences between String, StringBuffer, and StringBuilder
String Class Essentials:

Not a basic data type, but an object.
The bottom layer is an array of characters of type final.
The content of the string is immutable, any modification will generate a new String object.
Field definitions: private final char value[];
String concatenation operations:

When you use the + operator, you implicitly create a StringBuilder object and call the append method to splice it.
StringBuilder and StringBuffer Essentials:

Both inherit from the AbstractStringBuilder abstract class.
The bottom layer is all variable character arrays: char[] value;
For frequent string operations.
Thread safety:

StringBuffer is thread-safe, with synchronization locks added to the methods.
StringBuilder is non-thread-safe and does not have a synchronization lock.