Puge -- Summary of high frequency interview questions on Java Foundation (I)

Dear-JC 2022-05-22 12:05:57 阅读数:729

pugesummaryhighfrequencyinterview


One 、Java Basics

java Basic data type and packaging ?

Basic data type :

  • integer :byte short int long
  • floating-point :float double
  • Character :char
  • The logical model :boolean

Packaging :

  • Byte Short Integer Long Float Double Character Boolean
     Insert picture description here

The difference between overloading and rewriting ?

  • heavy load : Occurs in the same class , Method name must be the same , Parameter type 、 Number 、 Different order , And return value type , Access modifiers are irrelevant , Occurs at compile time .
  • rewrite : Occurs in a parent-child class , Method name 、 The parameter list must be the same , The return value range is less than or equal to the parent class , Exception range thrown is less than or equal to parent class , The access modifier range is greater than or equal to the parent class ; If the parent method access modifier is private Then the subclass cannot override the method .

String and StringBuffer、StringBuilder The difference between ?

  • variability :String Bottom use final Modify the , So it's immutable ;StringBuilder And StringBuffer It's a variable string ;
  • Security :String Objects in are immutable , It can be understood as a constant , Thread safety .StringBuffer Add a synchronization lock to the method or a synchronization lock to the called method , So it's thread safe .StringBuilder Method is not locked synchronously , So it's not thread safe .
  • performance : Every time the String When the type changes , Will generate a new String object , Then point the pointer to the new one String object , Low performance when modifying content .StringBuffer、StringBuilder The object itself is manipulated every time , Instead of generating new objects and changing object references ;StirngBuilder Will be more efficient , and StringBuffer Add synchronized keywords at the bottom of , There will be a drop in performance

So , Generally, when we operate on a small number of strings, we use String , Used when operating large amounts of data in a single threaded environment StringBuilder, In multithreading operations, a large amount of data is used StringBuffer.

== and equals?

  • == Judge whether two objects are the same object in memory . But if there are basic data types involved in the comparison , Whether the basic data types are compared with each other , Compare the basic data with their encapsulation class , They all compare values , Memory addresses are compared between reference data types .
  • equals(): It is also used to judge whether two objects are equal . But it generally has two uses , This class is not overridden equals() Method . Through equals() When comparing two objects of this class , It's equivalent to passing “==” Compare the two objects .
  • The other is class rewriting equals() Method , After rewriting, judge according to the rewriting logic . commonly , We all cover equals() Method to compare the contents of two objects ; If their contents are equal , Think that two objects are equal .

final、finally、finalize The difference between ?

  • final It's a security modifier , Just use final Modified classes cannot be inherited , use final Declared method cannot be overridden , Use final The declared variable is equivalent to a constant , Cannot be modified .
  • finally It's often used in exceptions , Namely try and cach After executing the code in , The method that must be carried out , We often finally There are some ways to close resources , close IO What's going on , Even if it is try and catch Are there in return Code , Will perform finally Of the contents of .
  • finalize yes Object class , This method of the recycled object is called when the garbage collector executes , It can be recycled by garbage .

The difference between abstract classes and interfaces ?

The same thing : Can't be instantiated , Can define abstract methods
Difference : In terms of design , Abstraction is the abstraction of a class , Is a template design ( Abstract type ), Interface is the abstraction of behavior , It's a code of conduct ( Abstract function ).

  • Interface to use interface Statement , Abstract class use abstract class Statement .
  • jdk1.7 All the methods declared in the previous interface are public Abstract method of ( There is no method body );jdk1.8 The default can be declared in the interface after / Static methods ;
    Abstract classes can declare not only abstract methods but also ordinary methods ( With method body )
  • The variables defined in the interface can only be public static constants , Constants and variables can be declared in abstract classes
  • Constructor... Cannot be declared in an interface , Constructors can be declared in abstract classes ( Used to be called by subclasses )

java Class loading mechanism

Class loading is JVM take .class File loaded into memory ( Class information is placed in the method area of the runtime data area , The heap area creates a java.lang.Class object , Used to encapsulate the data structure of the class in the method area ), And verify the data 、 Convert and initialize to form usable Java Class .

 Insert picture description here

What are the features of object-oriented ?

abstract : Abstract is the process of summing up the common characteristics of a class of objects to construct a class , Including data abstraction and behavior abstraction aspect . Abstractions only focus on the properties and behaviors of objects , Not paying attention to the details of these behaviors .
encapsulation : Encapsulation is often thought of as binding data to methods that manipulate data , Access to data can only be through defined The interface of . The essence of object-oriented is to describe the real world as a series of complete autonomy 、 Closed objects . The way we write in a class is to encapsulate the implementation details ; We write a class to encapsulate data and data operations . so to speak , Encapsulation is hiding everything that can be hidden , Only provide the simplest programming interface to the outside world
( For example, the difference between ordinary washing machines and automatic washing machines , Obviously, the automatic washing machine is better packaged, so it is easier to operate ).
Inherit : Inheritance is the process of obtaining inheritance information from existing classes and creating new classes . Classes that provide inheritance information are called superclasses ( super class 、 Base class ); A class that gets inheritance information is called a subclass ( Derived class ). Inheritance makes the changing software system have certain continuity , At the same time, inheritance is also an important means of variable factors in encapsulation .
polymorphism Polymorphism refers to allowing objects of different subtypes to respond differently to the same message . In short, we call the same method with the same object reference, but we do different things . Polymorphism is divided into compile time polymorphism and runtime polymorphism . If the object's method is regarded as the service provided by the object to the outside world , So runtime polymorphism can be interpreted as : When A System access B When the system provides services ,B There are many ways for the system to provide services , But everything is right A The system is transparent ( Like an electric razor is A System , Its power supply system is B System ,B The system can be battery powered or AC powered , It could even be solar ,A The system will only pass through B Class object calls the method of power supply , But I don't know what the underlying implementation of power supply system is , How to get the power ). Method overloading (overload) Implementation of compile time polymorphism ( Also known as front binding ), And method rewriting (override) Implementation of runtime polymorphism ( Also known as post binding ).

stay Java in , How to jump out of the current multiple nested loop ?

To get out of multiple loops , You can define a label before the outer loop statement , Then use the labeled... In the inner loop body code break sentence , You can jump out .
give an example :

ok:
for(int i=0;i<10;i++){

for(int j=0;j<10;j++){

system.out.println("i="+i+",j="+j);
if(j==5)
break ok;
}
}

java Class loading order ?

  1. All classes will load the base class first ;
  2. Initialization of static members takes precedence ;
  3. After member initialization , Will execute the construction method ;

Initialization of static members and execution of static blocks , Occurs when the class is loaded ; Members are loaded when the object is created ; Access to class objects and static blocks , Will trigger class loading .

What is reflection ?

Reflection In the running state , You can get all the information of any class or object ( Methods declared in class 、 attribute 、 Constructors ) And dynamically calling methods in objects 、 The mechanism of the constructor .
It's generally used Class clazz=Class.forName(“ Full path of class ”) This method , Get class object , And then through the class Object to get all the in the class method Method 、 attribute , call Method Of invoke Method can execute the method , But if it is Private method Words , Must pass getDeclaredMethod obtain , You also need to call the of the method setAccessible Set to true To execute .


Welcome to Yazheng message !!!

copyright:author[Dear-JC],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211806122011.html