Yesterday, I went to a company to interview for a Java development position. This article is mainly to make a record and summary of the interview.

The company is about 100-200 people in an acceptable environment and rents two floors of offices (31st and 32nd floors) in a building. Along with the elevator up there is a girl to apply for a testing position 🙂

The receptionist on the 31st floor told us to go upstairs to the receptionist on the 32nd floor. The receptionist on the 32nd floor asked us to go downstairs to the interviewer on the 31st floor. As a result, we were brought back downstairs to the 31st floor for the interview.

Fill out a personal information form and go straight to the interview (no written test 🙃).

Here are some interview questions:

1. Three main features of object orientation

A: Encapsulation, inheritance, polymorphism.

2. XML parsing

Answer: 1, analytic method

  • DOM: Document Object Model. This approach is recommended by the W3C as a standard way to handle XML.

Disadvantages: You must read the entire XML document to build the DOM model. If the XML document is too large, it wastes resources. Advantages: Suitable for manipulating data in XML (CRUD).

  • SAX: Simple API for XML. This approach, which is not an official standard, belongs to the open source community XML-Dev and is supported by almost all XML parsers.

2. Parsing tools

  • JAXP:

XML is parsed in DOM or SAX. The API is in the JDK.

  • Dom4J :(recommended)

Is the open source organization released parsing development package. (Ox, everyone is using it, including some of SUN’s technology implementations.)

3, reflection

A: When our program is running, we need to load some classes dynamically. These classes may not be used before and are not loaded into the JVM, but are loaded on demand at run time. This benefit is obvious to the server.

For example, we project the underlying sometimes with mysql, sometimes with oracle, need to be dynamically loaded driver classes according to actual condition, the reflection is useful, assuming that com. Java. Dbtest. MyqlConnection, Com. Java. Dbtest oracleConnection this two classes we need, then we can write more dynamic program, by Class tc = Class. Class.forname (” com. Java. Dbtest. TestConnection “); The full class name of the class lets the JVM find and load the class on the server, whereas if it is Oracle, the parameter passed in is a different one. This is where you can see the benefits of reflection, and the dynamic nature of Java!

For example, if you’ve been around Spring, you’ll see that when you configure various beans, you configure them as configuration files, and the spring container loads them dynamically according to your needs, and your application runs robustingly.

New Java 8 features

  • Lambda expressions: Lambda allows functions to be passed as arguments to a method.

  • Method references: Method references provide a very useful syntax for directly referencing Java class methods, object methods, or constructors.

Lambda expressions

Lambda expressions can be understood as concrete implementations of functional interfaces and their abstract methods.

Lambda expressions can be thought of as a special kind of anonymous inner class, and Lambda can only be used with functional interfaces.

Lambda syntax is as follows: ([parameter list, without data type]) -> {// execute statement [return..]. }Copy the code

The code is shown as follows:

public class TestLambda {  
     public static void main(String[] args) {  
           TestLambdaInterface1 t1 = new TestLambdaInterface1() {  
                @Override  
                public void test(a) {  
                     System.out.println("Use anonymous inner Classes"); }};// Same as the anonymous inner class above
           // The type on the right is automatically judged by the type on the left
           TestLambdaInterface1 t2 = () -> {  
                System.out.println("The use of lambda"); }; t1.test(); t2.test(); }}@FunctionalInterface  
interface TestLambdaInterface1 {  
     // An abstract method with no arguments
     void test(a);  
}  

Copy the code

6. Design patterns

The singleton pattern

Singleton pattern: Ensures that a class has only one instance and provides a global access point.

Implementation:

  • Use a private static variable, a private constructor, and a public static function.

  • Private constructors ensure that object instances cannot be created using constructors, only public static functions that return unique private static variables.

public class Singleton {

    private static Singleton instance;

    private Singleton(a) {}public static Singleton getInstance(a) {
        if (instance == null) {
            instance = new Singleton();
        }
        returninstance; }}Copy the code

The proxy pattern

Publish and subscribe model

The MVC pattern

Hibernate level 1 cache, level 2 cache

The Session cache is called Hibernate’s level 1 cache. SessionFactory’s external cache is called Hibernate’s second-level cache. Both caches are located in the persistence layer and hold copies of database data. SessionFactory’s built-in cache holds metadata and predefined SQL. SessionFactory’s built-in cache is read-only.

Caches are useful for:

  • Reduce database access frequency and improve access performance.

  • Ensure that objects in the cache are synchronized with the database, and objects in the cache are called persistent objects.

8. Can the key of ConcurrentHashMap be null?

  • A HashMap allows the insertion of null keys and null values

  • Neither HashTable nor ConcurrentHashMap can insert null keys or values

The thread name of the thread pool

The thread is created without passing in a name

ThreadPoolManager.potatoPool.execute(new MyThread());  
Copy the code

In this case, the thread pool automatically names the thread. If you want to change the name of the thread, you need to give the thread setName in the run method in the thread. As follows:

public class MyThread extends Thread{  
    public String threadName;  
    public MyThread (String threadName){  
        this.threadName=threadName;  
    }  
	
    @Override  
    public void run(a) { Thread.currentThread().setName(threadName); }}Copy the code

Create a thread and pass in the name directly

ThreadPoolManager.potatoPool.execute(new MyThread("aa"));  
Copy the code

MySQL installed on Linux, only local access, other machines can not access the problem

1, permission problem, modify the permission

2. Modify firewall configurations

11. Number of core threads (core) and maximum threads (Max)

When all core threads are working, the newly added task is added to the queue for processing, and if the queue is full, a new non-core thread is created to execute the task.

Thread pools catch exceptions

public class CaptureUncaughtException {  
    public static void main(String[] args) {  
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());  
        ExecutorService exec = Executors.newCachedThreadPool();  
        exec.execute(new ExceptionThread2());  ExceptionThread2 is the task object}}/ * * * MyUncaughtExceptionHandler: capture thread exception handling class, you need to implement * UncaughtExceptionHandler interface@author nnngu  
 */  
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {  
    @Override  
    public void uncaughtException(Thread t, Throwable e) {  
        System.out.println(t+"Exception caught, exception handled for:"+e); // e: the object thrown}}Copy the code

13. Mybatis Mapper

Reference:

Why mybatis mapper does not implement classes

Mybatis Mapper mapping file details

Jsp and servlets, nine built-in objects for Jsp

Servlet life cycle: Init, Service (doGet, doPost), deStory

JSP’s nine built-in objects:

Built-in object name type
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
exception Throwable
page Object(this)
out JspWriter
pageContext PageContext

JSP’s four domain objects:

object The domain
ServletContext The context field
HttpServletRequet Request the domain
HttpSession The session domain
PageContext Page domain

15. File upload, socket, stream

Reference:

Java file upload and download

16, Netty

Reference:

Netty – Basic usage introduction

17, collections,

Answer: List, Set, Map

Lists are ordered and can have repeating elements

Sets are unordered and do not allow duplicate elements

A Map is a key-value pair

IO and NIO

Reference:

Java NIO series tutorials

19. Write at the end

Finally, I noticed that the interviewer made a few mistakes, which I also noted here:

  • The interviewer said NIO was introduced after JDK 1.5. (NIO was introduced in JDK 1.4.)
  • When the number of threads in the thread pool reaches core, new tasks will continue to be created to handle them. (When the number of threads reaches the core number, new tasks are queued for processing, and threads are created only when the queue is full.)

Write here first and update later if there is anything to add.

This article is permanently updated at github.com/nnngu/Learn…