【JavaSE】Interview 01


努力经营当下 直至未来明朗!

1. The relationship between JDK, JRE and JVM

0
1

2. Supplement

1) The use of prefix and postfix ++. Postfix ++ will only perform ++ on itself after the expression ends.
2) & and | also represent logical operations if the expression result is boolean; but with && | | In comparison, they do not support short-circuit evaluation
3) ternary operator: expression 1 ? expression 2 : expression 3;
the results of expression 2 and expression 3 must be of the same type, unless type implicit typing can occur Conversion (pay attention to the final output result)
4) The type of parameters that cannot be used for switch: long float double boolean
complement: strings and enumerations (only available after 1.5), byte, char, short, and int can be used as parameters
5 ) The k-th power of 2 binary has only one 1, so: n=n & (n-1) ==0
6) Method signature: The final name of the method after being compiled and modified by the compiler .
The specific method is: the full path name of the method + parameter list + return value type, which constitutes the complete name of the method.
7) Find the number that appears only once and print it out (bitwise XOR): 0^n=n. n^n=0
8) Local variables in java must be initialized, otherwise an error will be reported! !
9) Two-dimensional array printing: Arrays.deepToString(数组名);
10) ① Execute the static code block first [it will be executed after the class is loaded]
② If there are multiple static ones, then look at the definition order (there will be overwriting)
③ If there is no instantiated object, then only Static ones will be executed (note: static ones will only be executed once)
⑤ Instance data [there are multiple instance objects, it depends on the order of definition]
⑥ Execution of constructor methods (ie: static blocks, instance blocks, constructor methods)
11) Local variables need to be initialized when used
12) Static and super do not coexist – super depends on the parent class object.
13) String string class is modified with final and cannot be inherited.
14) Overriding is a non-static, non-private and non-final modification of the parent class by the subclass. The implementation process of non-constructor methods must be rewritten, and the return value and formal parameter type cannot be changed .
15) Upward transformation: Subclass objects can be directly assigned to parent class objects. At this time, only methods and attributes in the parent class can be referenced, and the subclass's own cannot be called.

3. Interview question: What is the difference between overloading and rewriting?

  1. Rewriting rules]
    1) When a subclass overrides a parent class method, it must generally be consistent with the parent class method prototype: the modifier return value type and method name (parameter list) must be completely consistent 2) The overridden
    method return value type It can be different, but it must have a parent-child relationship (parent-child class relationship!!) 3) The access permissions
    of the subclass cannot be lower than the access permissions of the overridden methods in the parent class. For example: If a parent class method is modified by public, the overridden method in the subclass cannot be declared as protected. 4) Methods and constructors of the parent class that are modified by static, private, or final cannot be overridden. 5) The overridden method can be explicitly specified using the @Override annotation. This annotation can help us perform some legality checks. For example, if the method name is accidentally spelled wrong (such as aet), then the compiler will The compiler will find that there is no aet method in the parent class, and will compile and report an error, indicating that it cannot constitute an override.

  2. The difference between overwriting and overloading
    2
    is that method overloading is a polymorphic manifestation of a class, while method overriding is a polymorphic manifestation of a subclass and a parent class.

  3. Summary:
    Supplement [Method Rewriting]: Method return value, method name, parameter list (sequence, number, type) are consistent, static methods cannot be rewritten, private modified methods cannot be rewritten, and final modified methods cannot be rewritten. , the constructor cannot be overridden, and the access modifier of the subclass needs to be greater than or equal to the access modifier of the parent class
    (note: the return value type of the override can be different: the return value of the subclass and the return value of the parent class can also be in a parent-child class relationship[Covariant type]

4. super and this

  1. [Similar points]
    1) They are both keywords in Java
    2) They can only be used in non-static methods of a class to access non-static member methods and fields
    3) When called in a constructor, they must be in the constructor The first statement, and cannot exist at the same time
  2. [Differences]
    1) This is a reference to the current object, which is the object that calls the instance method. Super is equivalent to a reference to some members of the subclass object inherited from the parent class. 2)
    In non-static member methods, this is used To access the methods and properties of this class, super is used to access the methods and properties inherited from the parent class
    3) this is a hidden parameter of non-static member methods, super is not a hidden parameter
    4) In the constructor: this(...) is used To call the constructor of this class, super(...) is used to call the constructor of the parent class. The two calls cannot appear in the constructor at the same time.
    5) There must be a call to super(...) in the constructor, even if the user does not write a compiler. Increase, but this(...) will not exist if the user does not write it.

5. (Emphasis!!) If both the parent class and the subclass have static code blocks, instance code blocks, and no-argument constructors, the execution order in the inheritance relationship is:

1. Static code blocks of the parent class and subclass
(that is, the static code block of the parent class takes precedence over the static code block of the subclass and is executed earliest)
2. The parent class instance code block and construction method
3. The subclass instance code block and Constructor method
If the object is instantiated again with new, the static code block will not be executed because the static code block is only executed once

6. Static binding and dynamic binding

[Static binding]: Also called early binding (early binding), that is, at compile time, the specific method to be called is determined based on the type of actual parameters passed by the user. Typical representative: method overloading.
[Dynamic Binding]: Also known as late binding (late binding), that is, the behavior of the method cannot be determined at compile time. You need to wait until the program is running to determine the specific method of which class to call.

7. Understanding abstract classes

  1. Abstract classes do not necessarily contain abstract methods, but abstract methods belong to abstract classes.
  2. Note: Abstract classes are also classes, which can contain ordinary methods, properties, and even constructors.
  3. Abstract methods have no concrete implementation body
  4. Abstract methods cannot be private.
    Note: When an abstract method does not add an access qualifier, it defaults to public.
  5. Abstract methods cannot be modified by final and static, because abstract methods must be overridden by subclasses
  6. Abstract methods cannot be modified by private, static, final, etc., and cannot instantiate objects.
  7. Abstract classes can have constructors

8. Understanding the interface

  1. When a class overrides a parent class method, the modifier must be greater than or equal to the parent class's (note: the interface always omits public by default, so the subclass must add public modification when rewriting , otherwise a compilation error will be reported)
  2. Every method in the interface is a public abstract method, that is, the methods in the interface will be implicitly designated as public abstract (only yes public abstract, other modifiers will report an error)
  3. The interface can contain variables, but the variables in the interface will be implicitly designated as public static finalvariables
    (note: an initial value must be assigned; it cannot be modified subsequently and has the final attribute)
  4. There cannot be static code blocks and constructors in the interface
  5. Deep copy and shallow
    copy Shallow copy Cloneable: If the original object is changed, the copied object will also be affected - that is: the address does not change
    Deep copy: The two do not interfere with each other - the addresses are not the same - both classes and objects are copied

7. The difference between abstract classes and interfaces (important!)

Core difference: Abstract classes can contain ordinary methods and ordinary fields, and such ordinary methods and fields can be used directly by subclasses (without rewriting), while interfaces cannot contain ordinary methods, and subclasses must rewrite all abstract methods.

  1. What they have in common: None can be instantiated
  2. Differences:
    1) Abstract classes can contain non- abstract methods , while interfaces can only contain abstract methods.
    2) A class can only inherit one abstract class, but can implement multiple interfaces.
    3) Abstract classes can have constructors , but interfaces cannot have constructors.
    4) Abstract classes can have member variables , while interfaces can only have constants.
    5) Methods of abstract classes can have public, protected and default access modifiers , while methods of interfaces can only have public access modifiers. 6) Subclasses
    of abstract classes must implement all abstract methods, and classes that implement interfaces must implement all methods in the interface. 7) Abstract classes can have ordinary methods , but interfaces cannot have ordinary methods.
  3. Difference diagram
    7
  4. Note: Abstract methods have no method body! No curly braces either!
  5. Interfaces can have static member methods, but whether they are static member methods or default methods, they are all public modified (not abstract)
  6. There cannot be static, instance code blocks, or constructors in the interface. Abstract classes can have constructors, static blocks, and instance blocks.

8. The Object class has 12 member methods, namely

clone():Object、equals(Object):boolean、finalize():void、getClass()、hashCode():int、notify():void、notifyAll():void、toString():String、wait():void、wait(long):void、wait(long,int):void

9. Why should String be designed to be immutable? (Benefits of immutable objects?)

1) Conveniently implement string object pool . If String is mutable, then the object pool needs to consider the issue of copy-on-write.
2) Immutable objects are thread-safe .
3) Immutable objects are more convenient for caching hash codes , and can be saved into HashMap more efficiently when used as keys.

10. The difference between String, StringBuffer and StringBuilder

1) The content of String cannot be modified, but the contents of StringBuffer and StringBuilder can be modified. ( Content modifiable )
2) Most functions of StringBuffer and StringBuilder are similar ( function )
3) StringBuffer uses synchronous processing and is a thread-safe operation; while StringBuilder Synchronous processing is not used and it is a thread-unsafe operation ( processing and thread safety )

11. Abnormal system

1) Throwable : It is the top-level class of the exception system, which derives two important subclasses: Error and Exception
2) Error: refers to serious problems that cannot be solved by the Java virtual machine, such as: JVM internal errors and resource exhaustion Etc., typical representatives: StackOverflowError and OutOfMemoryError, once it occurs, there is no way to recover.
3) Exception: After the exception is generated, the programmer can handle it through the code to continue the program execution. The exception we usually call is Exception.
11

Guess you like

Origin blog.csdn.net/weixin_54150521/article/details/132365278
01