Summary of the latest java interview questions in 2018

1. The relationship between
JDK, JRE and JVM JDK, JRE, JVM relationship and difference.
JDK (Java Development Kit) is a product for Java developers and is the core of the entire Java, including the Java runtime environment JRE, Java tools and Java basic class libraries. Java language software development kit
JRE (Java Runtime Environment) is a collection of environments necessary to run JAVA programs, including JVM standard implementation and Java core class library.
JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine).

The relationship between the three 
JDK contains JRE, JRE contains JVM.

JVM: Convert bytecode files into machine instructions for a specific system platform. 
JRE: The core class library of JVM+Java language. 
JDK: JRE+Java development tool. JavaRuntime Environment

 

2. What does the "static" keyword mean? Is it possible to override a private or static method in Java?

The "static" keyword indicates that a member variable or member method can be accessed without the instance variable of the owning class.

Static methods in Java cannot be overridden because method overriding is dynamically bound at runtime, while static methods are statically bound at compile time. A static method is not associated with any instance of the class, so it is not conceptually applicable.

 

3. Is it possible to access non-static variables in static environment?

A static variable belongs to a class in Java, and its value is the same in all instances. When a class is loaded by the Java virtual machine, static variables are initialized. If your code tries to access non-static variables without an instance, the compiler will complain, because these variables have not yet been created and are not associated with any instance.

Two caveats for static methods:

1. Static methods cannot operate on non-static variables, nor can they call non-static methods. (This can be understood like this: static methods belong to classes and can be called directly through the class name, but there may not be any instances at this time, let alone operating instance variables and calling instance methods.)

2. The keywords this and super cannot be used in static methods. (same principle as above)

 

4. What is automatic unpacking?

Autoboxing is a conversion done by the Java compiler between primitive data types and the corresponding object wrapper types. For example: convert int to Integer, double convert to double, and so on. Otherwise, automatic unboxing

 

5. In Java, what is a constructor? What is constructor overloading? What is a copy constructor?

When a new object is created, the constructor is called. Every class has a constructor. In the case where the programmer does not provide a constructor for the class, the Java compiler will create a default constructor for the class.

Constructor overloading is very similar to method overloading in Java. Multiple constructors can be created for a class. Each constructor must have its own unique parameter list.

Java doesn't support copy constructors like C++ does, the difference is because Java doesn't create a default copy constructor if you don't write your own constructor

6. What are the basic interfaces of the Java collection class framework

Collection 
├List 
│├LinkedList 
│├ArrayList 
│└Vector 
│ └Stack 
└Set 
Map 
├Hashtable 
├HashMap 

└WeakHashMap

List and Set inherit from the Collection interface. Both of them are interface
Set
unordered and do not allow duplicate elements. HashSet and TreeSet are the two main implementation classes.
Lists
are ordered and allow repetition of elements. ArrayList , LinkedList and Vector are the three main implementation classes.
Map
also belongs to the collection system, but it has nothing to do with the Collection interface. Map is a set of mappings from key to value, where the key column is a set. The key cannot be repeated, but the value can be repeated. HashMap , TreeMap and Hashtable are the three main implementation classes. The SortedSet and SortedMap interfaces sort the elements according to the specified rules, and the SortedMap sorts the key column. 

 

7.JDBC

 

72. What is JDBC?

JDBC is an abstraction layer that allows users to choose between different databases. JDBC allows developers to write database applications in JAVA without having to care about the details of the underlying specific database.

73. Explain the role of the driver in JDBC.

The JDBC driver provides the implementation of the JDBC API interface class by a specific manufacturer. The driver must provide the implementation of the following classes in the java.sql package: Connection, Statement, PreparedStatement, CallableStatement, ResultSet and Driver.

74. What does the Class.forName() method do?

This method is used to load the driver that establishes the connection to the database.

75.What advantages does PreparedStatement have over Statement?

PreparedStatements are precompiled, therefore, the performance will be better. At the same time, PreparedStatement can be reused for different query parameter values.

76. When to use CallableStatement? What is the method used to prepare the CallableStatement?

CallableStatement is used to execute stored procedures. Stored procedures are stored and served by the database. Stored procedures can accept input parameters and return results. The use of stored procedures is highly encouraged as it provides security and modularity. The way to prepare a CallableStatement is:

1CallableStament.prepareCall();

 

8.java virtual machine (JVM)

<1jvm structure

1. Class Loader (ClassLoader) : Load the required class into the JVM when the JVM starts or when the class is running

2. Memory area (also called runtime data area): It is the memory area allocated by the operation when the JVM is running. The runtime memory area can be mainly divided into 5 areas (as shown in the figure)

3. Execution engine: responsible for executing the bytecode instructions contained in the class file

4. Native method interface : mainly calls the native method implemented by C or C++ and returns the result.

 

Garbage detection and recycling algorithms

The garbage collector generally has to do two things: detect garbage; collect garbage. How to detect garbage? There are generally the following methods ( garbage detection methods ):

Reference counting method : Add a reference counter to an object. Whenever there is a reference to it, the counter is incremented by 1; the reference is invalid and decremented by 1

Reachability analysis algorithm : Search with the root set object as the starting point. If any object is unreachable, it is a garbage object. The root set here generally includes objects referenced in the java stack, objects referenced in the method area Changliang pool

 

What are the methods of garbage collection in java? (recycling algorithm)

1. Mark-Clear:

The algorithm, like the name, is divided into two phases: marking and clearing. Mark all objects that need to be recycled, and then recycle them uniformly. This is the most basic algorithm, and subsequent collection algorithms are extended based on this algorithm.

Disadvantages: low efficiency; a lot of debris is generated after mark clearing

2. Replication algorithm:

This algorithm divides the memory space into two equal regions, only one of which is used at a time. During garbage collection, it traverses the currently used area and copies the objects in use to another area. This algorithm only processes the objects in use each time, so the cost of copying is relatively small, and at the same time, the corresponding memory can be sorted after copying, and there will be no "fragmentation" problem. Of course, the disadvantage of this algorithm is also obvious, that is, it requires twice the memory space

3. Mark-Organize

This algorithm is mainly to solve the problem of mark-sweep, which produces a large number of memory fragments; when the object survival rate is high, it also solves the efficiency problem of the replication algorithm. The difference is that when the object is cleared, the recyclable object is now moved to one end, and then the object outside the end boundary is cleared, so that there will be no memory fragmentation.

4.  Generational collection

Most of the current virtual machine garbage collection adopts this method, which divides the heap into the new generation and the old generation according to the life cycle of the object . In the new generation, due to the short lifespan of objects, a large number of objects will die each time they are recycled, so the replication algorithm is used at this time. Objects in the old generation have a higher survival rate and there is no additional space for allocation guarantees, so mark-collate can be used

Persistent generation: used to store static files, such as java classes, methods, etc. The persistent generation has no significant impact on garbage collection

 

Briefly describe the Java class loading mechanism?

The virtual machine loads the data describing the class from the Class file into the memory, and verifies, parses and initializes the data, and finally forms a java type that can be directly used by the virtual machine.

 

9. Frame

A brief introduction to spring springMvcspring-boot spring-cloud

what is spring

The definition of spring has been explained clearly and clearly from both the official and the market. I will simply define it as a lightweight Inversion of Control (IoC) and Aspect-Oriented (AOP) container, Java development framework, as for Inversion of Control, Aspect-Oriented, Lightweight, Containers, etc. Baidu, many big The cow explained it very clearly

 

what is springMvc

Here I will separate the nouns. Spring and mvc can better explain what springMvc is. MVC is a very common pattern developed for modern web projects. In short, C (controller) will V (view, user client) Terminal) and M (module, business) are separated to form MVC. The common development frameworks of mvc mode in the industry include Struts1, Struts2 and so on. Spring is an open source framework for professional development of web projects, and springMvc is an internal module link, which also adopts the mvc design pattern. So when using spring to develop web projects, MVC as the core link can use struts1/struts2/springMVc

 

what is spring-boot

My understanding is that there are too many products in the spring series family, so when using the spring integrated basic framework as the project structure, most junior staff spend a lot of time building the project and may not be able to understand it. spring-boot was born to solve the pain point of developers. To put it bluntly, it automatically encapsulates the previous manual configuration process and provides default configuration. The characteristics of the Daniel summary are:

 

Simple and easy to use, both beginners and Daniel can easily get started, and the annotations will provide convenience for users;

Spring boot has well encapsulated and integrated third-party technologies and provided a large number of third-party interfaces;

Can be automatically configured by dependencies, no configuration files such as XML are required

It also provides features such as security, let's ignore it for now.

 

Now the boot summary is that you can start quickly, build the project quickly, save a lot of time and energy in the configuration file ring, fool you into the project door, and write business logic. Now it is integrated with many frameworks to configure according to the specifications and write handwritten code in minutes

 

what is spring-cloud

Microservices are a hot topic in the current architecture field. If you want to know what spring-cloud is, please first figure out what microservices are. spring-colud is a cloud distributed architecture solution. Based on spring boot, it can become a microservice in spring cloud with less configuration in spring boot. It's a bit high-sounding and I haven't used it, but it is simply understood as: spring cloud provides some commonly used distributed components, all of which are application-oriented, just like spring mvc.

 

The difference between Mybatis and Hibernate

hibernate: is a standard ORM framework (Object Relational Mapping). The entry threshold is relatively high, there is no need to write SQL, and SQL statements are automatically generated, so it is difficult to optimize and modify SQL statements.

Application scenario: suitable for small and medium-sized projects with little change in requirements, such as: background management system, erp, orm, oa, etc.

mybatis: Focus on sql itself. Programmers need to write sql statements by themselves. It is more convenient to modify and optimize sql. Mybatis is an incomplete ORM framework. Although programmers write sql themselves, mybatis can also implement mapping (input mapping, output mapping)

Application Scenario: Applicable to projects with changing needs, such as Internet projects.

 

10. Webservice service (interface)

 

2. WebService classification (divided into two ways):

<1.soapWebservice

1. Three elements of soapwebService

One of SOAP, WSDL (WebServicesDescriptionLanguage), and UDDI (UniversalDescriptionDiscoveryandIntegration), soap is used to describe the format of transmitting information, SOAP is Simple Object Access Protocol (Simple ObjectAccess Protocol),

WSDL is used to describe how to access specific interfaces,

uddi is used to manage, distribute, and query webService.

2. Three elements of soapwebService 2

Performance comparison of several popular Webservice frameworks

Several popular frameworks: Apache Axis1, ApacheAxis2, Codehaus XFire, Apache CXF, etc.

Keywords: Axis1, Axis2, XFire, CXF, Spring, SOAP, StAX, WSDL

 

<2.restfulwebservice

One is the SOAP protocol method, in which WSDL, UDDI, etc. are required, and the second is the RESfull method, which does not require WSDL, UDDI, etc. at all. And the REST approach now seems to be the more popular and promising approach.


What are the design forms you know? 

Answer: There are generally 23 design forms in Java, and we don't need all of them, but several commonly used design forms should be mastered. Everything is listed above in design form. I have listed the design forms that need to be mastered by myself. Of course, the more I can master, the better.

Generally speaking, the design forms are divided into three categories:

There are five types of founding forms: factory method form, general factory form, singleton form, builder form, and prototype form.

There are seven stereotype forms: adapter form, decorator form, proxy form, appearance form, bridge form, combination form, and flyweight form.

There are eleven types of behavioral modes: strategy mode, template method mode, observer mode, iteration sub mode, responsibility chain mode, command mode, memorandum mode, mode mode, visitor mode, mediator mode, and interpreter mode.


The difference between wait and seek methods in Java

Answer: The biggest difference is that wait releases locks while waiting, while sleep does not release object locks. wait is usually used for inter-thread interaction, and sleep is usually used to suspend execution.


What is a thread pool and how to use it? 

Answer: The thread pool is to put multiple thread objects into a container in advance. When it is used, it is not necessary to create a new thread, but to directly go to the pool to get the thread.


The difference and connection between process and thread

definition:

Process: It is an instance of program running and an independent unit for resource allocation and scheduling by the system. It includes an independent address space, resources and one or more threads.

Thread: It can be regarded as a lightweight process and is the basic unit of CPU scheduling and dispatch.

the difference:

(1) Scheduling: The thread is the basic unit of scheduling and allocation, and the process is the basic unit of owning resources.

(2) Concurrency: Not only processes can execute concurrently, but also multiple threads of the same process can execute concurrently.

(3) Owning resources: A process is an independent unit that owns resources. Threads do not own system resources, but can access resources belonging to the process.

(4) System overhead: When creating or revoking a process, because the system has to allocate and reclaim resources for it, the system overhead is significantly greater than the overhead of creating or revoking threads.

Tell me about your understanding of Java reflection

The JAVA reflection mechanism is that in the running state, for any class, you can know all the properties and methods of the class; for any object, you can call any of its methods; this dynamically obtained information and dynamic call object The function of the method is called the reflection mechanism of the java language.

Starting from the object, the complete information of the class can be obtained through reflection (Class class) (class name Class type, the package in which it is located, the Method[] type of all methods it has, the complete information of a method (including modifiers, return value types, exception, parameter type), all properties Field[], complete information of a property, Constructors), calling properties or methods of a class

My own summary: Get all the information about classes, objects, and methods during operation.

There are three ways to get a Class object:

1. Through the getClass() method of the Object class. E.g:

Class c1 = new String("").getClass();

2. Implemented through the static method of the Class class - forName():

Class c2 = Class.forName("MyObject");

3. If T is a defined type, in java, its .class file name: T.class represents the matching Class object, for example:

Class c3 = Manager.class;

Class c4 = int.class;

Class c5 = Double[].class;






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325516458&siteId=291194637