Summary of JAVA interview questions with answers, java interview 2020, 2021

1. What is the difference between JDK and JRE?
JRE is the abbreviation of Java Runtime Environment. As the name implies, it is the Java runtime environment, which includes the Java virtual machine and the Java basic class library. It is the software environment required for the running of programs written in java language. It is provided to users who want to run java programs. There are also class files of all Java class libraries, all in the lib directory and packaged into jars.
 Jdk is the abbreviation of Java Development Kit. As the name suggests, it is a Java development kit. It is a development kit required for programmers to write Java programs in the Java language and is provided to programmers. JDK includes JRE, as well as javac, a compiler for compiling java source code, as well as many tools for debugging and analyzing java programs: jconsole, jvisualvm and other tools, as well as documentation and demo example programs required for writing java programs. .

2. What is the difference between == and equals?
Data types in java and ""Meaning:
1. Basic data types: byte, short, char, int, long, float, double, boolean. For
comparison between them, the double equal sign (
), the comparison is their value.
2. Reference data type:
When they compare with (==), they compare their storage address in memory (to be precise, the heap memory address).
Note: For the second type, unless it is the same new object, the result of their comparison is true, otherwise the result of the comparison is false. Because every new time, the heap memory space will be reopened

3. Are two objects with equal hashcodes == equal? Is equals equal? Conversely [two objects are compared equal with equals, then their hashcodes] are equal?
HashCode is an inherent method of all java objects. If it is not overloaded, what is returned is actually the memory address of the object on the heap of the jvm, and the memory address of different objects is definitely different, so this hashCode is definitely different. If overloaded, due to the algorithm used, it may cause the hashCode of two different objects to be the same

By default:
1. During the execution of a Java application, when the hashCode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used in the equals comparison of the object has not been modified. From one execution of an application to another execution of the same application, the integer does not need to be consistent.
2. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of these two objects must generate the same integer result.
3. If the two objects are not equal according to the equals(java.lang.Object) method, then calling the hashCode method on any of the two objects does not necessarily generate different integer results. However, programmers should be aware that generating different integer results for unequal objects can improve the performance of hash tables.

4. What is the role of final in Java?
a) Final can modify the class, such a class cannot be inherited.
b) Final can modify methods, such methods cannot be overridden.
c) final can modify variables, the value of such variables cannot be modified, they are constants.
5. How much is Math.round(-1.5) in Java?
1.floor returns the largest integer
that is not greater than 2.round is the calculation of 4 rounding to 5, when rounding is to the integer greater than it's
round method, it means "rounding", the algorithm is Math.floor(x+0.5), That is, add 0.5 to the original number and then round down. Therefore, the result of Math.round(11.5) is 12, and the result of Math.round(-11.5) is -11.
3. ceil is not less than the smallest integer

Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

6. Is String a basic data type?
String in java is an object and a reference type. The difference between a basic type and a reference type is that the basic type only represents simple characters or numbers, and the reference type can be any complex data structure, and the basic type only represents simple data types, reference types Can represent complex data types, and can also manipulate the behavior of this data type.
The java virtual machine handles basic types and reference types in a different way. For basic types, the java virtual machine allocates the memory space actually occupied by the data type. For reference type variables, it is just a pointer to a certain heap area. Pointer to the instance.
String is not a basic data type, but a class, which is a string in programming languages ​​such as C++ and Java.
The String class is immutable. Any change to the String class will return a new String class object. String object is an ordered collection of System.Char objects, used to represent strings. The value of the String object is the content of the ordered collection, and the value is immutable.
Because the java.lang.String class is of final type, it cannot be inherited or modified.
In order to improve efficiency and save space, we should use the StringBuffer class.
The eight basic data types of Java are:
integer type byte, short, int, long
floating point type double, float
logic type boolean
text type char
7. What types of manipulation strings are available in Java? What is the difference between them?
1. Find the string indexOf(String s), lastIndexOf(String str)
String str = "We are students";
int size = str.indexOf("a"); // The value of the variable size is 3
2. Use the charAt() method to return the character at the specified index.
String str = "hello word";
char mychar = str.charAt(5); // the result of mychar is w

Get the
substring substring (int beginIndex)
This method returns the substring that starts at the specified index position and knows the end of the string.

String str = "Hello word";
String substr = str.substring(3); //Get the string, at this time the substr value is lo word
1
2
substring(int beginIndex, int endIndex)
beginIndex: the index to start intercepting the substring Position
endIndex: the end position of the substring in the entire string
String str = "Hello word";
String substr = str.substring(0,3); //The value of substr is hel
1
2
Remove the space and the
trim() method returns A copy of the string, ignoring leading and trailing spaces.

String replacement
replace (oldchar, newChar) method can be realized to replace the specified character or string with a new character or string
oldChar: the character or string to be replaced
newChar: used to replace the content of the original string
if you want to replace The character oldChar appears multiple times in the string, and the replace() method will replace all oldChar with newChar. It should be noted that the case of the oldChar character to be replaced should be consistent with the case of the characters in the original string.

String str = "address";
String newstr = str.replace("a", "A");// The value of newstr is Address
1
2
Determine the start and end of the string
startsWith() method and endsWith() method are used separately To judge whether the string starts or ends with the specified content. The return values ​​of these two methods are boolean types.

  1. startsWith(String prefix)
    This method is used to determine whether the prefix of the current string object is the string specified by the parameter.
  2. endsWith(String suffix)
    This method is used to determine whether the current string ends with a given substring

3. Determine whether the strings are equal
equals (String otherstr)
If the two strings have the same characters and length, use equals () method to compare, return true. At the same time, the equals() method is case sensitive when comparing.
equalsIgnoreCase(String otherstr) The
equalsIgnoreCase() method is the same as the equals() type, but the case is ignored when comparing.
Compare two strings lexicographically. The
compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the string. The character sequence represented by this String object is compared with the parameter string in lexicographical order. The indicated character sequences are compared. If the String object is lexicographically before the parameter string, the comparison result is a negative integer; if the String object lexicographically is after the parameter string, the comparison result is a positive integer; if the two strings are equal, The result is 0.
str.compareTo(String otherstr);
1 The toLowerCase() method of a
letter case conversion
string can rewrite all characters in the string from uppercase letters to lowercase letters, and the tuUpperCase() method can change the string Lowercase letters in are rewritten to uppercase letters.

str.toLowerCase();
str.toUpperCase();

String splitting
Use the split() method to split the string according to the specified separator character or string, and store the split result in the character array.

str.split(String sign);
1
sign is the separator for dividing the string, and regular expressions can also be used.
There is no uniform symbol for dividing the string. If you want to define multiple separators, you can use the symbol "|". For example, ",|=" means that the delimiters are "," and "=".

str.split(String sign, in limit);
1
This method can split the string according to the given separator and limit the number of splits

Get the length of
the string length() convert the string to the corresponding numeric value
int type Integer.parseInt(string)
double type Double.valueOf(string).doubleValue()
Convert
a character between string array and string A string can be turned into a character array, and similarly, a character array can be turned into a string.
The following operation methods are provided in the String class:
change a string into a character array:
public char[] toCharArray()
public String(char[] value)
public String(char[] value,int offset,int count)

public class StringAPIDemo01{ public static void main(String args[]){ String str1 = "hello"; // Define a string char c[] = str1.toCharArray(); // Turn a string into a character array for( int i=0;i<c.length;i++){ // Loop output System.out.print(c[i] + “,”); } System.out.println(""); // Line feed String str2 = new String©; // change all character arrays to String String str3 = new String(c,0,3); // change part of character arrays to String System.out.println(str2); // output characters String System.out.println(str3); // output string ) };












13
Conversion of character string to byte array The
byte array (byte array) is often used in general IO operations.
The following methods are provided in the String class to convert between a string and a byte array: a
string becomes a byte array: public byte[] getBytes()
turns a byte array into a string:
all bytes The array becomes String: pbulic String(byte[] bytes)
Part of the byte array becomes String: public String(byte[] bytes,int offset,int length)

public class StringAPIDemo03{ public static void main(String args[]){ String str1 = "hello"; // Define the string byte b[] = str1.getBytes(); // Turn the string into a byte array System.out .println(new String(b)); // change all byte arrays into strings System.out.println(new String(b,1,3)); // change part of byte arrays into strings ) };






8. Is String str="i" the same as String str=new String("i")?
9. How to reverse the string?
10. What are the commonly used methods of the String class?
11. Does an abstract class have to have abstract methods?
12. What are the differences between ordinary and abstract classes?
13. Can abstract classes use final modification?
14. What is the difference between an interface and an abstract class?
15. How many types of IO streams are in Java?
16. What is the difference between BIO, NIO and AIO?
17. What are the common methods of Files?

container

18. What are the Java containers?
19. What is the difference between Collection and Collections?
20. What is the difference between List, Set and Map?
21. What is the difference between HashMap and Hashtable?
22. How to decide whether to use HashMap or TreeMap?
23. Tell me about the implementation principle of HashMap?
24. Tell me about the implementation principle of HashSet?
25. What is the difference between ArrayList and LinkedList?
26. How to realize the conversion between array and List?
27. What is the difference between ArrayList and Vector?
28. What is the difference between Array and ArrayList?
29. What is the difference between poll() and remove() in Queue?
30. Which collection classes are thread safe?
31. What is Iterator?
32. How to use Iterator? What are the characteristics?
33. What is the difference between Iterator and ListIterator?
34. How to ensure that a collection cannot be modified?

Multithreading

35. What is the difference between parallel and concurrency?
36. The difference between thread and process?
37. What is a daemon thread?
38. What are the ways to create threads?
39. What is the difference between runnable and callable?
40. What are the statuses of threads?
41. What is the difference between sleep() and wait()?
42. What is the difference between notify() and notifyAll()?
43. What is the difference between thread run() and start()?
44. What are the ways to create a thread pool?
45. What are the statuses of the thread pool?
46. ​​What is the difference between submit() and execute() methods in thread pool?
47. How to ensure the safe operation of multiple threads in a Java program?
48. What is the upgrade principle of multithreaded locks?
49. What is a deadlock?
50. How to prevent deadlock?
51. What is ThreadLocal? What are the usage scenarios?
52. Tell me about the underlying implementation principle of Synchronized?
53. What is the difference between Synchronized and Volatile?
54. What is the difference between Synchronized and Lock?
55. What is the difference between Synchronized and ReentrantLock?
56. Tell me about the principle of Atomic?

reflection

57. What is reflection?
58. What is Java serialization? Under what circumstances need serialization?
59. What is a dynamic agent? What are the applications?
60. How to implement dynamic proxy?
Object copy

61. Why use clone?
62. How to implement object cloning?
63. What is the difference between deep copy and shallow copy?

Java Web

64. What is the difference between JSP and Servlet?
65. What are the built-in objects of JSP? What are the roles?
66. What are the four scopes of JSP?
67. What is the difference between Session and Cookie?
68. Tell me about the working principle of Session?
69. If the client bans Cookies, can Session still be used?
70. What is the difference between Spring MVC and Struts?
71. How to avoid SQL injection?
72. What is XSS attack and how to avoid it?
73. What is a CSRF attack and how to avoid it?

abnormal

74. The difference between throw and throws?
75. What is the difference between final, finally, finalize?
76. Which part of try-catch-finally can be omitted?
77. In try-catch-finally, if the catch is returned, will finally be executed?
78. What are the common exception classes?

The internet

79. What do the HTTP response codes 301 and 302 represent? What's the difference?
80. The difference between Forward and Redirect?
81. Briefly describe the difference between TCP and UDP?
82. Why does TCP need a three-way handshake, can't it work twice? why?
83. Tell me how TCP sticky packets are generated?
84. What are the seven-layer models of OSI?
85. What are the differences between Get and Post requests?
86. How to achieve cross-domain?
87. Tell me about the implementation principle of JSONP?

Design Patterns

88. Tell me about the design patterns you are familiar with?
89. What is the difference between a simple factory and an abstract factory?

Spring/Spring MVC

90. Why use Spring?
91. Explain what is AOP?
92. Explain what is IOC?
93. What are the main modules of Spring?
94. What are the commonly used injection methods in Spring?
95. Are Beans in Spring thread safe?
96. What kind of Bean scope does Spring support?
97. What are the ways for Spring to automatically assemble Bean?
98. What are the implementation methods of Spring transaction?
99. Tell me about Spring's transaction isolation?
100. Tell me about the Spring MVC running process?
101. What are the components of Spring MVC?
102. What is the role of @RequestMapping?
103. What is the role of @Autowired?

Spring Boot/Spring Cloud

104. What is Spring Boot?
105. Why use Spring Boot?
106. What is the Spring Boot core configuration file?
107. What types of Spring Boot configuration files are there? What is the difference between them?
108. What are the ways Spring Boot can achieve hot deployment?
109. What is the difference between JPA and Hibernate?
110. What is Spring Cloud?
111. What is the function of Spring Cloud circuit breaker?
112. What are the core components of Spring Cloud?

Hibernate

113. Why use Hibernate?
114. What is the ORM framework?
115. How to view the printed SQL statement in the console in Hibernate?
116. How many query methods does Hibernate have?
117. Can Hibernate entity classes be defined as final?
118. What is the difference between using Integer and int for mapping in Hibernate?
119. How does Hibernate work?
120. The difference between get() and load()?
121. Tell me about Hibernate's caching mechanism?
122. What are the statuses of Hibernate objects?
123. What is the difference between getCurrentSession and openSession in Hibernate?
124. Does Hibernate entity class have to have a parameterless constructor? why?

Mybatis

125. What is the difference between #{} and ${} in Mybatis?
126. How many paging methods does Mybatis have?
127. Is RowBounds query all results at once? why?
128. What is the difference between Mybatis logical paging and physical paging?
129. Does Mybatis support lazy loading? What is the principle of lazy loading?
130. Tell me about the first level cache and the second level cache of Mybatis?
131. What are the differences between Mybatis and Hibernate?
132. What executors does Mybatis have?
133. What is the realization principle of Mybatis paging plug-in?
134. How does Mybatis write a custom plug-in?

RabbitMQ

135. What are the usage scenarios of RabbitMQ?
136. What are the important roles of RabbitMQ?
137. What are the important components of RabbitMQ?
138. What is the role of VHost in RabbitMQ?
139. How are RabbitMQ messages sent?
140. How does RabbitMQ ensure the stability of messages?
141. How does RabbitMQ avoid message loss?
142. What are the conditions to ensure the success of message persistence?
143. What are the disadvantages of RabbitMQ persistence?
144. How many broadcast types does RabbitMQ have?
145. How does RabbitMQ implement a delayed message queue?
146. What is the use of RabbitMQ cluster?
147. What are the types of RabbitMQ nodes?
148. What issues should be paid attention to when constructing RabbitMQ cluster?
149. Is each node of RabbitMQ a complete copy of other nodes? why?
150. What happens if the only disk node in the RabbitMQ cluster crashes?
151. Does RabbitMQ have any requirements for the stopping order of cluster nodes?

Kafka

152. Can Kafka be used separately from ZooKeeper? why?
153. How many data retention strategies does Kafka have?
154. Kafka has set 7 days and 10G to clear data at the same time. By the fifth day, the message reached 10G. What will Kafka do at this time?
155. What conditions will cause Kafka to run slower?
156. What should I pay attention to when using Kafka cluster?

ZooKeeper

157. What is ZooKeeper?
158. What are the functions of ZooKeeper?
159. How many deployment modes does ZooKeeper have?
160. How does ZooKeeper ensure the synchronization of the master and slave nodes?
161. Why is there a master node in the cluster?
162. There are 3 servers in the cluster and one of the nodes is down. Can ZooKeeper still be used at this time?
163. Tell me about the notification mechanism of ZooKeeper?

MySQL

164. What are the three paradigms of the database?
165. There are a total of 7 pieces of data in an auto-increment table. The last 2 pieces of data are deleted, the MySQL database is restarted, and another piece of data is inserted. What is the ID at this time?
166. How to obtain the current database version?
167. What is ACID?
168. What is the difference between Char and VarChar?
169. What is the difference between Float and Double?
170. What is the difference between internal connection, left connection and right connection of MySQL?
171. How is the MySQL index implemented?
172. How to verify whether the MySQL index meets the demand?
173. Talk about the transaction isolation of the database?
174. Tell me about the engines commonly used in MySQL?
175. Tell me about MySQL row lock and table lock?
176. Talk about optimistic locking and pessimistic locking?
177. What are the methods for MySQL troubleshooting?
178. How to optimize the performance of MySQL?

Redis

179. What is Redis? What are the usage scenarios?
180. What are the functions of Redis?
181. What is the difference between Redis and MemeCache?
182. Why is Redis single-threaded?
183. What is cache penetration? How to deal with it?
184. What data types does Redis support?
185. What are the Java clients supported by Redis?
186. What is the difference between Jedis and Redisson?
187. How to ensure the consistency of cache and database data?
188. There are several ways of Redis persistence?
189. How does Redis implement distributed locks?
190. What are the defects of Redis distributed lock?
191. How does Redis optimize memory?
192. What are the Redis elimination strategies?
193. What are the common performance problems of Redis? How to solve it?

JVM

194. Tell me about the main components of JVM? And its role?
195. Tell me about the JVM runtime data area?
196. Tell me about the difference between stacks?
197. What are queues and stacks? What's the difference?
198. What is the parent delegation model?
199. Tell me about the execution process of class loading?
200. How to judge whether the object can be recycled?
201. What reference types are there in Java?
202. Tell me about what garbage collection algorithms the JVM has?
203. Tell me about what garbage collectors the JVM has?
204. Tell me more about the CMS garbage collector?
205. What are the new generation garbage collectors and the old generation garbage collectors? What's the difference?
206. Briefly describe how the generational garbage collector works?
207. Tell me about the JVM tuning tools?
208. What are the commonly used JVM tuning parameters?

Guess you like

Origin blog.csdn.net/qq_39493521/article/details/112570831
Recommended