Java-Interview Questions (1)

The basic class of file reading?
FileReader and FileWriter inherited from Reader and Writer respectively. FileReader is used to read files, and FileWriter is used to write files. Both need to call their own construction methods to create objects, and then call their respective read() and write() methods to read and write operations

What is serialization?
Serialization is a flow of processing objects, which is to stream objects. It can read and write to the streamed object, and it can also be transmitted over the network. The realization of serialization requires the serialized object to implement the serializable interface (the interface does not have a method that needs to be implemented, mainly to mark the class can be instantiated), and then use the output stream such as FileOutputStream to construct the ObjectOutputStream object stream, and then use the writeObject( of ObjectOutputStream) ) Method can be serialized, read using the input stream

What is the difference between statement and preparedStatement in java? The difference between
preparedStatement and statement is that its statements are pre-compiled, and runtime parameters can be used, which has higher security and better performance.

Mybatis execution process
1. Create SqlsessionFactory
2. Create Sqlsession through SqlsessionFactory
3. Perform database operations through Sqlsession
4. Call Sqlsession.commit() to submit the transaction
5. Call Sqlsession.close() to close the session

What are the shortcomings of jdbc? How does mybatis solve it?
1. Frequent creation and release of jdbc and database links cause a waste of system resources.
Mybatis can configure the database connection pool in sqlmapconfig.xml, and use the connection pool to configure the database connection.
2. jdbc frequently modifies the sql statement, which is not easy to maintain.
Mybatis can put the sql statement in the mapping xml file, which is separated from the java code.
3. The jdbcsql statement is not easy to pass parameters, and a lot of SQL needs to be modified to pass parameters.
mybatis automatically maps java objects to sql statements, reducing sql changes and placeholder debugging
4. result set parsing is not easy
mybatis automatically maps sql execution results to java objects

What are the similarities and differences between mybatis and hibernate?
1. Mybatis is not a complete ORM framework, because it needs to manually write sql statements and execute them after mapping, and then encapsulate the execution results into java objects.
2. Mybatis is easy to learn and write sql statements manually. The way can strictly control SQL execution performance and flexibility. It is suitable for Internet and enterprise operation software that does not require high relational data models, but mybatis cannot achieve database independence. If you need to be compatible with multiple databases, you need to configure a large number of configuration files.
In short, it can be made according to user needs in a limited environment. A framework with good scalability and maintainability is a good framework, so a suitable framework is the best

Mybatis's primary cache and secondary cache?
Mybatis's primary cache is turned on by default. When the same sqlsession executes the same sql statement, the data will be queried from the database for the first time and placed in the cache. The next time it is executed, it will be taken directly from the cache. , If an addition, deletion, or modification operation occurs, the sqlsession cache will be deleted. If you don't want to use it, you can configure the scope of the first level cache as statement in sqlmapconfig.xm.
Mybatis's second-level cache is closed by default and needs to be configured in sqlmapconfig.xml. The main reason is that query sql under the same namespace can get data from the cache. This is a bitch, and it is recommended to use a cache database such as Redis.

The difference between collection and collection
Collection is the upper-level interface of collection, and the main inheritance of it is set and list. Collections is a helper class for collections, providing a series of static methods to search and sort collections and other operations.

The difference between heap and stack
Stack is a stack, a linear collection, each thread contains a stack area (there is only one heap area so all threads are shared), and the stack operates in a first-in-first-out manner. The stack memory will allocate dedicated memory space when the program enters a method to store the internal local variables of this method. Released when the method ends. The created object will be put into the heap area and will not be released when the method ends.

Does an array have a length() method? What about strings?
Arrays do not have a length() method, but have a length attribute, and strings have a length() method.

How does jvm load class files?
The loading of classes in jvm mainly depends on classloader and its subclasses. Java classloader is an important java runtime system component, which is responsible for finding and loading files at runtime.

Mvc details?
Mvc is short for model view controller. Model is the data view is the view layer controller is the business

Why stop and suspend are not recommended? The
stop() method will directly stop the current thread and release all the acquired locks. If the monitored object is in an incoherent state, other threads can modify and check the object, naturally Not safe. suspend() will stop the current thread, but will continue to hold the object lock, other threads cannot access it, so deadlock is prone to occur.

The difference between wait method and sleep method The
wait method will cause the thread to enter the waiting state, release the object lock, and enter the waiting pool waiting for the object. The notify method needs to be started. The sleep method will cause the thread to stop running for a period of time, enter the sleep state, will not release the object lock, and automatically start after the set time.

The thread synchronization method
wait makes a thread enter the waiting state and release the lock of the object it holds. sleep makes a thread enter the sleep state without releasing the lock it holds. notify wakes up a thread in a waiting state. allnotify wakes up all threads in a waiting state.

The difference between string and stringbuffer The
string class is not inheritable, and the provided string is immutable. The string provided by stringbuffer can be modified, in other words, if you know that the string needs to be modified, it is recommended to use stringbuffer

The basic state of the
thread Thread refers to a program unit that executes the program code during the running of the program, and each program has at least one thread.
Four states: ready to run, hang, die, if you use sleep, one more sleep

gc means garbage disposal, and memory disposal is the most troublesome thing for programmers. The gc function provided by java can automatically monitor whether the object exceeds the scope, so as to achieve automatic recovery.
The method to start a thread is the start() method, which will make the thread enter the ready state and can be called by the jvm. The run() method is the method associated with this thread. When the j2ee server starts, it will establish a quantitative connection, and always detect no less than this number. When the client needs it, the connection pool will look for idle connections, mark them as busy and return. If there are no free connections, the connection pool will create a certain amount of connections based on the configuration file. After the call is completed, the connection will be marked as idle. When the java program violates the semantic rules of java, the jvm will indicate the error that will occur as an exception. There are two main types of Java semantic rules. One is the built-in semantic rules of the Java class library, such as the null pointer exception. One is defined by the programmer and can be freely referenced through the throw keyword at any time. All exceptions are subclasses of throwable


The operations of acid atomic transactions are either all executed or all fail.
Isolation When the database is operated with multiple transactions, each transaction does not interfere with each other and is isolated from each other.
The data before the consistent transaction modification is consistent with the data that the transaction has modified enough.
The operations performed by persistent transactions on database data are persistent, even if the database is damaged.

HashMap is a lightweight implementation of HashTable (not thread-safe), so the speed of HashMap may be better than HashTable, and HashMap has removed the contains method of HashTable and changed it to contains value and contains key. HashMap inherits from the dictionary class, and HashTable is an implementation of Map interface.

What declares the existence of a method without implementing it is called an abstract class and cannot be created. Abstract classes cannot have abstract constructors and abstract static methods. If a subclass of an abstract class does not implement its methods, then this subclass is also an abstract class. Interface is a variant of abstract class. Interfaces are all abstract methods, and their instantiation method is similar to that of abstract classes. The interface can only define static and final variables that are constants. Instanceof can determine whether a certain class implements a certain interface.

The collection can only store objects, even if it is stored in the int class, it is transformed into an integer class, and it stores a reference to the object, and can also store an unlimited number of data types of indefinite types. The underlying data structure is an array of
low efficiency increase and delete, query and modify high efficiency
can store null values
thread-safe, high efficiency
indexed, to facilitate the retrieval
element can be repeated, we ourselves can go through repeated selection sort
can not be sorted, But it can be sorted by the Collection.sort() method

Some interview questions that I have summarized, feel wrong, please advise.

Guess you like

Origin blog.csdn.net/qq_36008278/article/details/114906660