Interview Java frequently asked questions summary

  

  It ’s another year of gold, silver, and silver. With the resumption of work in various industries, everyone has also started a wave of job hunting. As a teacher of excellent employment Java training, it also solves some of the problems in the interview for many students. So today Give you a brief introduction to the problems you will encounter in the Java interview.

  1. JAVA's cross-platform principle

  The source code is compiled to generate bytecode files, and the bytecode files are translated into corresponding machine code through different versions of JVMs downloaded on different platforms. Note that cross-platform Java programs are not JVMs. The JVM is developed using C / C ++, which is compiled bytecode and cannot be cross-platform.

  2. What is the difference between int and Integer?

  1), Integer is a packaging class of int, int is a basic data type of java 2), Integer variable must be instantiated before it can be used, but int variable does not need 3), Integer is actually a reference to an object, when new Integer, actually generates a pointer to this object; and int directly stores the data value 4), the default value of Integer is null, the default value of int is 0

  3. Life cycle of Java objects

  Creation phase, application phase, invisible phase, unreachable phase, collection phase, finalization phase, object space reallocation phase, etc.,

  4. The storage principle of Map or HashMap

  HashMap is composed of a structure of array + linked list, specific reference: HashMap implementation principle

  5. Detailed explanation of JVM structure principle and GC working mechanism

  Specific reference: JVM structure, detailed explanation of GC working mechanism. When it comes to GC, remember two points: 1. GC is responsible for reclaiming all memory space without any referenced objects. Note: The garbage collection is the memory space occupied by the object without any reference instead of the object itself. 2. Two algorithms of the GC recycling mechanism, a. Reference counting method b. Reachability analysis algorithm (reachability here, You can see what cycle of basic 2 Java objects),

  6. JAVA object-oriented features?

  Encapsulation, inheritance, polymorphism, and abstract encapsulation: reflected by classes, encapsulating entities into classes, which contain attributes and methods

  Inheritance: inheritable features between classes, making code reuse

  Polymorphism: Referencing different subclasses by passing to the parent class object to exhibit different behaviors

  Abstraction: Abstract the common characteristics of a class of entities and encapsulate them in an abstract class.

  7. The difference between final, finally, and finalize

  final:

  1. Modifier (keyword) If a class is declared as final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared as abstract or final.

  2. Declaring variables or methods as final, you can e68a84e8a2ade799bee5baa6e79fa5e9819331333365666235 to ensure that they will not be changed in use. Variables declared as final must be given an initial value at the time of declaration, and can only be read and not modified in future references, and methods declared as final can also be used only, and cannot be overloaded.

  finally:

  Provide finally blocks to perform clear operations during exception handling. If an exception is thrown, the matching catch statement will be executed, and then control will enter the finally block, if any.

  finalize:

  Is the method name. Java technology allows the use of finalize () method to do the necessary cleanup before the garbage collector removes the object from memory. This method is called when the garbage collector determines that the object being cleaned is not referenced.

  Finalize is defined in the Object class, so all classes inherit it. Subclasses can override the finalize () method to organize system resources or perform other cleanup tasks.

  8. Does Java have goto?

  goto is a reserved word in java language, which has not been used in java

  Goto is used in conjunction with conditions in the C language for operations such as jumping out of loops. Java is a structured programming language. Using goto in Java will cause many unnecessary troubles. Goto has not yet been used. Java requires simple and convenient

  9. What is the difference between error and exception?

  Both Exception and Error inherit the Throwable class. In Java, only instances of the Throwable type can be thrown (Throw) or caught (Catch). It is the basic component type of the exception handling mechanism.

  Exception is an unexpected situation that can be expected during the normal operation of the program. It may and should be caught and handled accordingly.

  Error refers to a situation that is unlikely to occur under normal circumstances. Most errors will cause the program to be in an abnormal and unrecoverable state. Since it is abnormal, it is not convenient and does not need to be captured. For example, the common OutOfMemoryError is a subclass of Error.

  The Exception class is divided into checkable exceptions and unchecked exceptions. Checkable exceptions must be captured in the source code for capture processing. This is part of the compile-time check.

  Unchecked exceptions are so-called runtime exceptions, similar to NullPointerException, ArrayIndexOutOfBoundsException, etc., which are usually logical errors that can be coded to avoid.

  10. When the A and B fields in the data table are indexed, will using A or B alone have an indexing effect?

  When looking at the combined index of A and B, who is in front and who is in the back, if A is in front, then using A alone will have an index effect, and using B alone will not, and vice versa. Similarly, when using like fuzzy query, if you only use the first%, then there is an index effect, if you use double% matching, then there is no index effect

  11. Does List, Set, Map inherit from Collection interface?

  List and Set are interfaces inherited from the Collection interface. Set does not allow duplicate items. List allows duplicate items. The classes derived from the Set interface are TreeSet, HashSet, and LinkedHashSet. The classes derived from the List interface are ArrayList, Vector, etc. Map is an independent interface and does not inherit the Collection interface.

  12. Is it possible to inherit the String class?

  "No, because the String class has a final modifier, and the final modified class cannot be inherited, and the implementation details are not allowed to change."

  13. In distributed and cluster environments, how to refresh the cache and how to keep it synchronized?

  A. How to refresh the cache? 1. Regular refresh 2. Active refresh coverage. Each cache framework has its own refresh mechanism, or cache invalidation mechanism. Take Redis and Ehcache as examples. They all have their own expiration mechanism. In addition, when actively refreshing the coverage, you only need to obtain the corresponding key to overwrite the data.

  B. How to keep the cache synchronized? This redis has its own cluster synchronization mechanism, that is, the replication function. Ehcache also has a distributed cache synchronization configuration, only need to configure different server addresses.

  13. How to prevent the submission of new tasks?

  By changing the state of the thread pool to STOP, when the execution executes the task again, if it is tested that the state is not RUNNING, then rejectedExecution is thrown, thereby achieving the purpose of preventing the submission of new tasks.

  14. What is the difference between String, StringBuilder and StringBuffer?

  (1) Running speed: StringBuilder> StringBuffer> String

  String is a string constant. The other two are string variables. String objects cannot be changed after they are created. The operation on String is actually a process of continuous creation and recycling, and the execution speed is slow.

  (2) Thread safety: StringBuilder is thread-insecure, while StringBuffer is thread-safe

  The StringBuffer object can use the synchronized keyword when the string buffer is used by multiple threads to ensure thread safety.

  (3) Use scenarios

  String is suitable for a small number of string operations; StringBuilder is suitable for a single thread in the case of a large number of operations in the string buffer; StringBuffer is suitable for multi-threaded in the case of a large number of operations in the string buffer.

  15. What is the difference between == and equals?

  (1) Comparison between basic data types, using double equal signs (==), comparing the values ​​of the two

  (2) Comparison between compound data types (classes), comparing addresses in the memory pool, using equals. In the Java language, usually equals is given to the developer to define, what conditions meet the Object is equals.

  16.What are the Java containers?

  The container of JAVA includes the following:

  List,Map,Set,Collection,List,LinkedList,ArrayList,Vector,Stack,Set

  Map,Hashtable,HashMap,WeakHashMap

  17. What are the ways to create threads?

  There are 4 ways to create threads:

  1. Inheriting the Thread class (the real thread class) is the implementation of the Runnable interface.

  2. Implement the Runnable interface and rewrite the run method inside

  3. Applications can use the Executor framework to create thread pools. The Executor framework is an implementation of the thread pool provided in juc.

  4. Implement the Callable interface to create a Thread thread through the FutureTask wrapper

  18. What is reflection?

  In the Java runtime environment, for any class, can I know what properties and methods this class has? For any object, can I call any of its methods

  The Java reflection mechanism mainly provides the following functions:

  1. Determine the class to which any object belongs at runtime.

  2. Construct an object of any class at runtime.

  3. Determine the member variables and methods of any class at runtime.

  4. Call any object method at runtime.

  19.What is the function of shutdownNow ()?

  Preventing the submission of new tasks and interrupting the currently running thread, that is, the thread in the worker. In addition, it will remove the tasks in the workQueue, and add these tasks to the list to return.

  20. Why does it have no effect on the submitted tasks?

  When calling the method of interrupting a task, it will detect the task in the worker. If the task corresponding to the worker is not interrupted and is an idle thread, it will interrupt it. In addition, the values ​​in the workQueue are still sent to the works in a certain logical order, so that you can ensure that the submitted tasks are executed according to the logic of the thread itself and are not affected.

  The above are the common questions in the interview summarized by the excellent employment Java training teacher. I hope you will have some help for the interview.

Guess you like

Origin www.cnblogs.com/jiuq521/p/12702780.html