当前位置:网站首页>Record (III)

Record (III)

2022-06-10 22:16:00 Li_ XiaoJin

I saw some interview questions on the Internet , Sort out the answers by yourself , Check for defects and make up for omissions .

Java Basics

1.0 JAVA What are the basic data types in , How many bytes does each take up .

byte, short, int, long, float, double, char, boolean

data type

byte

boolean

1 bit, Less than a byte

byte

8 bit,1 byte

short

16 bit,2 byte

char

16 bit,2 byte

int

32 bit,4 byte

float

32 bit,4 byte

long

64 bit,8 byte

double

64 bit,8 byte

1.1 String Can classes be inherited , Why? .

String Class cannot be inherited , because String Like final Modifier ,final Modified classes cannot be inherited .

1.2 String,Stringbuffer,StringBuilder The difference between .

String String constant StringBuffer String variable ( Thread safety ) StringBuilder String variable ( Non-thread safety )

In a nutshell , String The type and StringBuffer The main performance difference of the type is String It's an immutable object , So in every time to String When a type is changed, a new one is created String object , Then point the pointer to the new one String object , So it's better not to use the string that often changes the content String , Because every time an object is generated, it has an impact on system performance , Especially when there are many unreferenced objects in memory , JVM Of GC Will start working , That's going to be quite slow . And if you use StringBuffer Classes are different , I get it right every time StringBuffer The object itself operates , Instead of generating new objects , Then change the object reference . So we recommend it in general StringBuffer , Especially if string objects change frequently . And in some special cases , String Object string concatenation is actually by JVM Explain a StringBuffer Object splicing , So these days String The object's speed is no faster than StringBuffer Object is slow , Especially in the following string object generation , String Efficiency is far more than StringBuffer fast : String S1 = “This is only a” + “ simple” + “ test”; StringBuffer Sb = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”); You will be surprised to find , Generate String S1 The object's speed is just too fast , And this time StringBuffer I can't believe the speed is superior at all . Actually, this is JVM A trick of , stay JVM Eyes , This String S1 = “This is only a” + “ simple” + “test”; In fact, that is : String S1 = “This is only a simple test”; So of course, it doesn't take much time . But what we should pay attention to here is , If your string is from another String Object words , Not so fast , for example :String S2 = “This is only a”;String S3 = “ simple”;String S4 = “ test”;String S1 = S2 +S3 + S4; Now JVM Will behave in the same way In most cases StringBuffer > StringStringBufferJava.lang.StringBuffer Thread safe variable character sequence . A similar to String String buffer for , But it can't be modified . Although at any point in time it contains a certain sequence of characters , But some method calls can change the length and content of the sequence . String buffers can be used safely for multiple threads . These methods can be synchronized if necessary , So all operations on any particular instance seem to happen in serial order , This sequence is consistent with the sequence of method calls made by each thread involved .StringBuffer The main operation on is append and insert Method , These methods can be overloaded , To accept any type of data . Each method effectively converts the given data into a string , Then append or insert the character of the string into the string buffer .append Method always adds these characters to the end of the buffer ; and insert Method to add characters at a specified point . for example , If z To quote a current content is “start” String buffer object for , Then this method calls z.append("le") Will cause the string buffer to contain “startle”, and z.insert(4, "le") Will change string buffer , To include “starlet”. In most cases StringBuilder > StringBufferjava.lang.StringBuildejava.lang.StringBuilder A variable character sequence is 5.0 Newly added . This class provides a StringBuffer Compatible API, But there's no guarantee of synchronization . This class is designed to be used as StringBuffer A simple replacement of , When the string buffer is used by a single thread ( It's very common ). If possible , It is recommended that such , Because in most implementations , It is better than StringBuffer Be quick . The two methods are basically the same .

1.3 ArrayList and LinkedList What's the difference? .

  • ArrayList, Using array data structure List, Create an array a, You can access the data by adding an index or a subscript , An array is a continuous piece of data in its contents , Can support random access .a Represents the memory address of the array , The index is the offset of the data position from the first element , Such as a[0] Represents the current first element , and a Refers to a location , So anywhere , It only takes two steps , find a The location of , Then get the offset to access the data , The time complexity is O(1). When creating an array, you need to specify the length ,ArrayList It can be increased all the time because when the length is exceeded , A new array will be created , Copy the original data , Then discard the old array .ArrayList Support random access , Realized RandomAccess Interface . Use when there are many queries ArrayList, When you need to delete data , Subsequent data corners of the current data are found to have changed , So time complexity is O(n-i), Therefore, it is suitable for multiple queries , With few additions and deletions .
  • LinkedList, Using linked list data structure List, Random... Is not supported , Length is not specified when creating , When used, the system allocates memory , So the location in memory is random .LinkedList When adding data, not only the current data will be recorded , It also records the position of the last element , So access this element through the previous element , The elements point to each other to form a chain like structure . When you need to access data at a location , Only through the first element , Step by step to find , The time complexity is O(n), No random access . therefore Query efficiency is slow , When deleted , Just delete the data , The next element points to the previous element , The time complexity of deletion is O(1), So it is suitable for frequent additions and deletions .

For details, please refer to a previous article : About one time List Interview It was written before ArrayList Related content :ArrayList Capacity expansion mechanism of

1.4 Let's talk about the instantiation order of classes , For example, static data of parent class , Constructors , Field , Subclass static data , Constructors , Field , When new When , Their order of execution .

In sequence :

  1. Parent static variables 、
  2. Parent static code block 、
  3. Subclass static variables 、
  4. Subclass static code block 、
  5. The parent class is not a static variable ( Parent instance member variable )、
  6. Parent constructor 、
  7. Subclass nonstatic variables ( Subclass instance member variable )、
  8. Subclass constructor .

Code validation can look at this :Java Basics - Class instantiation order

1.5 Which have been used? Map class , What's the difference ,HashMap Is it thread safe , Used concurrently Map What is it? , What are their internal principles , For example, storage mode ,hashcode, Capacity expansion , Default capacity, etc .

Used to HashMap、LinkedHashMap、TreeMap、 Specific to see This note ( 3、 ... and ) - Java aggregate HashMap Not thread safe , And issue that it should be used ConcurrentHashMap,

1.6 JAVA8 Of ConcurrentHashMap Why give up the segmented lock , Is there anything wrong with that? , If you design , How do you design .

1.7 Is there any order Map Implementation class , If there is , How do they keep order .

Hashmap and Hashtable It's not orderly .

TreeMap and LinkedHashmap It's all orderly .(TreeMap The default is key Ascending ,LinkedHashmap The default is data insertion order )

TreeMap It's based on a comparator Comparator To achieve orderly .

LinkedHashmap It is based on the linked list to achieve orderly data insertion .

1.8 The difference between abstract classes and interfaces , Can a class inherit multiple classes , Can interfaces inherit multiple interfaces , Class can implement multiple interfaces .

I wrote it before , Click on here see

1.9 What's the difference between inheritance and aggregation .

  • Inherit It refers to a class ( Called subclass 、 A subinterface ) Inherit another class ( Called the parent class 、 The parent interface ) The function of , And the ability to add its own new features , Inheritance is the most common relationship between classes or interfaces ; stay Java This kind of relationship is based on the keyword extends Clearly mark , There is generally no controversy in the design ;
  • polymerization Aggregation is a special case of association , He embodies the whole and the part 、 The relationship you have , namely has-a The relationship between , At this time, the whole and the part are separable , They can have their own life cycle , Parts can belong to more than one whole object , It can also be shared for multiple whole objects ; For example, computers and CPU、 The relationship between the company and its employees ; At the code level , And the relationship is consistent , It can only be distinguished from the semantic level ;

stay Family Class contains a Child. And contains Child Of get,set Method , You can just create Family Then through the constructor or get,set to Child assignment

Reference resources : Inherit 、 Realization 、 rely on 、 relation 、 polymerization 、 The connection and difference of combination

2.0 IO What are the models , Tell me what you understand nio , He and bio,aio What's the difference between , Talk about reactor Model .

To be specific, see Interview questions IO What are the models , Tell me what you understand nio , He and bio,aio What's the difference between , Talk about reactor Model .

2.1 The principle of reflection , What are the three ways reflection creates class instances .

You can see this about reflection Reflect relevant content

  • Mode one Through the getClass Method to get . This approach requires specific classes and objects of that class , And call getClass Method . Class class2 = foo1.getClass(); System.out.println(class1==class2);//true'
  • Mode two Any data type ( Including basic data types ) All have a static attribute class, Through it, you can directly get the corresponding Class object . This approach uses concrete classes , Then call the static properties in the class class complete , There is no need to call methods , Better performance . Class class1 = Foo.class;
  • Mode three adopt Class.forName() Method to get . This method only needs to use the class name , You can get the Class object , More conducive to expansion . ``` Class class3 = null; try { class3 = Class.forName("com.imooc.reflect.Foo"); } catch (ClassNotFoundException e) { e.printStackTrace(); }

System.out.println(class2==class3);//true

```

2.2 In reflection ,Class.forName and ClassLoader difference .

stay java in Class.forName() and ClassLoader Can be loaded on the class .

difference :

  1. Class.forName In addition to the class .class File loading to jvm Outside of China , It also explains the class , In the execution class static block .
  2. and classloader Do only one thing , Will be .class File loading to jvm in , Not execute static The content in , Only in newInstance To execute static block .

I read the articles in Jianshu carefully , In reflection ,Class.forName and ClassLoader difference

2.3 Describes several implementations of dynamic proxy , State the relative advantages and disadvantages respectively .

2.4 Dynamic proxy and cglib The difference of realization .

2.5 Why? CGlib Method can implement proxy to interface .

2.6 final Use of .

  1. By final Decorated classes cannot be inherited
  2. By final The decorated method cannot be overridden
  3. By final Modified variables cannot be changed

And then there is :

  1. By final The method of decoration ,JVM Will try to find inlining for it , This is for ascension Java Efficiency is very important . therefore , If you can be sure that the method will not be inherited , Then try to define the method as final Of
  2. By final Constant decorated , In the compilation phase, it will be stored in the constant pool of the calling class

2.7 Write three kinds of singleton pattern implementation .

Look at it in detail The singleton pattern of design pattern

2.8 How to do all the things automatically in the parent class for the child class hashcode and equals Realization ? What are the advantages and disadvantages of doing so .

2.9 Please combine OO Design concept , Talk about the visit modifier public、private、protected、default Role in application design .

3.0 The difference between deep copy and shallow copy .

3.1 Data structure description of array and linked list , Their time complexity .

3.2 error and exception The difference between ,CheckedException,RuntimeException The difference between .

3.3 Please list 5 Runtime exceptions .

ClassCastException( Class conversion exception ) IndexOutOfBoundsException( An array ) NullPointerException( Null pointer ) ArrayStoreException( Data storage exception , The types are inconsistent when manipulating arrays ) also IO Operation of the BufferOverflowException abnormal

3.4 In your own code , If you create a java.lang.String class , Can this class be loaded by class loader ? Why? .

stay 《 In depth understanding of java virtual machine 》 There is a passage in this book ,“ Even if you customize your own classloader , Force to use defineClass() Method to load a ‘java.lang’ The first class will not succeed , If you try to do this , You will receive a message thrown by the virtual machine itself ‘java.lang.SecurityException:Prohibited package name:java.lang’ abnormal ”. So if you create a java.lang.String Class , Can't be loaded by the classloader . Because the virtual opportunity throws an exception .

Speaking of class loading , You must be very familiar with the class loading mechanism .

Class loading uses the parental delegation model , When you want to load a class , You must first give your parent loader , It will find a way to load , If it fails to load , Then tell us , We'll do it ourselves .

therefore , stay java in java.lang.String It must be on the upper floor ClassLoader Has been loaded , So you don't have a chance to load it yourself .

3.5 Say you are right java.lang.Object In the object hashCode and equals Understanding of methods . In what scenarios do you need to re implement these two methods .

3.6 stay jdk1.5 in , Generics are introduced , The existence of generics is used to solve what problems .

Generics are mainly aimed at the security risks caused by downward transformation , Its core component is when declaring classes or interfaces , Do not set the type of parameter or property .

3.7 In this way a.hashcode() What's the usage? , And a.equals(b) What does it matter .

3.8 Is it possible 2 Different objects have the same hashcode.

There may be , Two unequal objects may have the same hashcode value , Is that why hashmap There will be conflicts . equal hashcode The value is only specified if two objects are equal , have to Must have the same hashcode value , But there is no rule about unequal objects .

3.9 Java Medium HashSet How the interior works .

4.0 What is serialization , How to serialize , Why serialize , What's wrong with deserialization , How to solve .

4.1 java8 New features .

Java8 Added a lot of features , We mainly discuss the following :

  • Lambda expression − Lambda Allows you to take a function as an argument to a method ( The function is passed to the method as an argument ).
  • Method reference − Method references provide a very useful Syntax , You can directly quote the existing Java Class or object ( example ) A method or constructor of . And lambda A combination of , Method reference can make the construction of language more compact and concise , Reduce redundant code .
  • The default method − The default method is a method that has an implementation in the interface .
  • New tools − New compiler tools , Such as :Nashorn engine jjs、 Class dependency Analyzer jdeps.
  • Stream API − Newly added Stream API(java.util.stream) Introduce the real functional programming style to Java in .
  • Date Time API − Strengthen the processing of date and time .
  • Optional class − Optional Class has become Java 8 Part of the class library , Used to solve null pointer exception .
  • Nashorn, JavaScript engine − Java 8 Provides a new Nashorn javascript engine , It allows us to JVM Run specific javascript application .

JVM

4.2 When will stack memory overflow occur .

4.3 JVM Memory structure ,Eden and Survivor The proportion .

4.4 JVM Why is memory divided into new generations , Old age , Lasting generation . Why should we divide the new generation Eden and Survivor.

4.5 JVM Once complete GC How is the process , How to promote the object to the old age , Tell me about some of the main JVM Parameters .

4.6 You know what kind of garbage collector , Advantages and disadvantages of each , Let's focus on cms and G1, Including the principle of , technological process , Advantages and disadvantages .

4.7 The principle of garbage collection algorithm .

It's written before Garbage collection algorithm

4.8 When there is a memory overflow , How do you get it wrong .

4.9 JVM How much do you know about the memory model , Like reordering , Memory barrier ,happen-before, Main memory , Working memory, etc .

5.0 Let's talk about the classloader you know , Can we break the parental delegation , How to break .

5.1 Tell me about JAVA The reflection mechanism of .

5.2 Your online application JVM What are the parameters .

5.3 g1 and cms difference , Throughput first and response first garbage collector selection .

5.4 How to call the route stack information .

Open source framework

5.5 In a nutshell tomcat structure , And its class loader process , Thread model, etc .

5.6 tomcat How to tune , What parameters are involved .

Tomcat Tuning and JVM Parameter optimization springboot built-in tomcat Parameter tuning

5.7 Tell me about Spring Loading process .

5.8 Spring AOP Implementation principle of .

5.9 Tell me about Spring Propagation properties of the transaction .

6.0 Spring How to manage affairs .

6.1 Spring How to configure transactions ( Say something specific about the key xml element plain ).

6.2 Tell me about your right Spring The understanding of the , The principle of non singleton Injection ? Its life cycle ? Principle of cyclic injection ,aop Implementation principle of , say something aop Some of the terms in , How they work with each other .

6.3 Springmvc in DispatcherServlet Initialization process .

6.4 netty Thread model of ,netty How to base on reactor Implemented on the model .

6.5 Why choose netty.

6.6 What is? TCP Sticky package , unpacking . What is the solution .

6.7 netty Of fashwheeltimer Usage of , Realization principle , Whether the call is not on time , How to solve .

6.8 netty How to deal with the heartbeat under the weak network .

6.9 netty What is the communication protocol of .

7.0 springmvc Notes used , What is the role , principle .

7.1 springboot Activation mechanism .

  • 1. adopt SpringFactoriesLoader load META-INF/spring.factories⽂ Pieces of , Get and create SpringApplicationRunListener object
  • 2. Then from SpringApplicationRunListener To issue starting news
  • 3. Create a parameter , And configure the current SpringBoot What the app will use Environment
  • 4. When it's done , Still by SpringApplicationRunListener To issue environmentPrepared news
  • 5. establish ApplicationContext
  • 6. initialization ApplicationContext, And set up Environment, Load related configuration, etc
  • 7. from SpringApplicationRunListener To issue contextPrepared news , inform SpringBoot Applied ApplicationContext Already prepared OK
  • 8. Various kinds beans Loading and loading ApplicationContext, Continue by SpringApplicationRunListener To issue contextLoaded news , inform SpringBoot Applied ApplicationContext Already loaded OK
  • 9.refresh ApplicationContext, complete IoC The last available container ⼀ Step
  • 10. from SpringApplicationRunListener To issue started news
  • 11. Complete the start of the final program
  • 12. from SpringApplicationRunListener To issue running news , Tell the program is running

operating system

7.2 Linux What kernel parameters have you paid attention to under the system , Tell me what you know .

This is more complicated Linux Kernel parameter configuration Linux Kernel parameter optimization

7.3 Linux Next IO There are several models , What is the meaning of each .

7.4 epoll and poll What's the difference? .

Classic interview questions ---select、poll、epoll What's the difference between

7.5 What do you usually use Linux command .

head、tail、tar、ssh、sftp、telnet、curl、wget、cp、mv、sed、su cd、ls、grep、find、rm、kill、ps、cat、chown、chmod、

7.6 Look at the last five lines of the file with one line of command .

tail -5f filename

7.7 Output the running... With a single command java process .

ps -ef|grep java

7.8 Introduce your understanding of the thread switching process in the operating system .

7.9 Difference between process and thread .

8.0 top What's after the command , What's the role .

8.1 on-line CPU Blasting height , How do you find the problem .

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/2020-12-04-17-52-38

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102054352500.html