Java Technical interview Problem collation

Source: Internet
Author: User
Tags mutex

1. Common classes for Java

String class

System information can be obtained

StringBuilder string Tool class

Thread Threading Class

Math-related tool classes

ArrayList the set of the underlying array implementation

LinkedList the set of the underlying list implementation

An implementation class for the HashMap interface map

An implementation class for the HashSet interface set

Scanner Simple Text Scanner

Calendar Date Class

Date Day Class

File directory or Files operation class

FileInputStream input Stream

FileOutputStream output stream

PrintWriter

Bufferedinputstream with buffered input stream

Bufferedoutputstream with buffered output stream

BufferedReader reads text from the character input stream, buffering individual characters,

2. Java common run-time exceptions (5 runtime exceptions common to Java)

Java runtime Exceptions are exceptions that may be thrown when a Java virtual machine is working correctly

Java provides two exception mechanisms, one is run-time exceptions (runtimexception) and one is an inspection exception (checked exception)

Check exception: The IO exception that we often encounter and the SQL exception are check-in exceptions. For this exception, the Java compiler requires that we catch these exceptions, so Mina will have to write a bunch of catch to catch these exceptions, whether we like it or not.

Run-time exception: we can not handle it. When such an exception occurs, it is always taken over by the virtual machine. For example: No one has ever dealt with a nullpointerexception exception, which is a run-time exception, and this exception is one of the most common exceptions.

Runtimexception under the Java.lang package, here is a runtime exception provided by the Java Virtual machine:

Annotationtypemismatchexception,

ArithmeticException,

Arraystoreexception,

Bufferoverflowexception,

Bufferunderflowexception,

Cannotredoexception,

Cannotundoexception,

ClassCastException,

Cmmexception,

Concurrentmodificationexception,

Domexception,

Emptystackexception,

Enumconstantnotpresentexception,

Eventexception,

IllegalArgumentException,

Illegalmonitorstateexception,

Illegalpathstateexception,

IllegalStateException,

Imagingopexception,

Incompleteannotationexception,

Indexoutofboundsexception,

Jmruntimeexception,

Lsexception,

Malformedparameterizedtypeexception,

Mirroredtypeexception,

Mirroredtypesexception,

MissingResourceException,

Negativearraysizeexception,

Nosuchelementexception,

Nosuchmechanismexception,

NullPointerException,

Profiledataexception,

ProviderException,

Rasterformatexception,

Rejectedexecutionexception,

SecurityException,

SystemException,

Typeconstraintexception,

Typenotpresentexception,

Undeclaredthrowableexception,

Unknownannotationvalueexception,

Unknownelementexception,

Unknowntypeexception,

Unmodifiablesetexception,

Unsupportedoperationexception,

Webserviceexception

4 Common run-time exceptions:

1, ClassCastException (class conversion exception)

2, Indexoutofboundsexception (array out of bounds)

3. NullPointerException (null pointer)

4. Arraystoreexception (data store exception, type inconsistency when manipulating array)

5, bufferoverflowexception (IO operation exception)

6. Rasterformatexception (format conversion error)

7, Unknowntypeexception

3. Spring Dependency Injection mechanism

1, Set Value method injection (setterinjection)

To complete a dependency setting through the setter method of the class

2. Construction sub-injection (constructorinjection)

Construct sub-injection, that is, to complete the dependency setting through the constructor function

4. Interfaces and abstract classes are different

The concepts of interfaces and abstract classes are different. An interface is an abstraction of an action, and an abstract class is an abstraction of the root.

The abstract class represents what this object is. The interface represents what this object can do. For example, men, women, these two classes (if it's a class ...) ), their abstract class is human. Description, they are all human. People can eat, dogs can eat, you can define "eat" as an interface, and then let these classes to implement it.

Therefore, in high-level languages, a class can inherit only one class (abstract class) (just as people cannot be both biological and non-living), but can implement multiple interfaces (eating interfaces, walking interfaces).

1th. An interface is a variant of an abstract class, and all methods in an interface are abstract. Abstract classes are classes that declare the existence of a method without implementing it.

2nd. Interfaces can inherit multiple, abstract class not

3rd. An interface definition method cannot be implemented, whereas an abstract class can implement a partial method.

4th. The basic data type in the interface is static and the suction class is not.

When you focus on the nature of a thing, use an abstract class, and when you focus on an operation, use an interface.


Abstract classes are much more powerful than interfaces, but the cost of defining abstract classes is high. Because of the high-level language (which is actually designed), each class can inherit only one class. In this class, you must inherit or write all of its subclasses.

All commonalities. Although the interface is much weaker in functionality, it is only a description of an action. And you can implement multiple interfaces in one class at the same time. In the design phase will reduce the difficulty.

5. How threads are implemented

1, is by inheriting the thread class way, the thread class can not inherit other classes to implement other functions, the way to implement the interface does not have this limitation;

Class MyThread extends Thread

2. The most important point is that the effect of resource sharing can be achieved by implementing the Runnable interface.

Class MyThread implements Runnable

6. Status of the thread

The state transition of a thread is the basis of thread control. The total thread state can be divided into five states: Raw, dead, can run, run, wait/block. This is described in a diagram as follows:

650) this.width=650; "src=" http://img1.51cto.com/attachment/200809/200809131221320062031.png "border=" 0 "style=" Padding:0px;margin:0px;vertical-align:top;border:none; "/>

1. New state: The thread object has been created and the start () method has not been invoked on it.

2. Operational status: When the thread is eligible to run, but the scheduler has not selected it as the state in which the thread is running. When the start () method is called, the thread first enters the operational state. After the thread has run or has returned from blocking, waiting, or sleeping, it returns to the operational state.

3. Running state: The thread Scheduler selects a thread from a running pool as the current thread is in the state of the thread. This is also the only way that a thread goes into a running state.

4. Wait/block/Sleep state: This is the state at which the thread is eligible to run. In fact, this three-state combination is one, and its common denominator is that the thread is still alive, but there is no condition to run at the moment. In other words, it is operational, but if an event occurs, he may return to the operational state.

5. Death state: When the thread's run () method finishes, it is considered dead. This thread object may be alive, but it is not a separate thread. Once a thread dies, it cannot be resurrected. If you call the start () method on a dead thread, the java.lang.IllegalThreadStateException exception is thrown.


thread synchronization methods include: mutexes, read-write locks, condition variables, semaphores, and tokens.

Mutex and read-write locks: provide protection against critical resources, and when multithreading attempts to access critical resources, you must access the critical resources by acquiring locks. (Critical resource: A resource that is shared by multiple threads) when the read-write thread acquires the lock frequency is not very different, generally adopt the mutex, if the read thread accesses the critical resource more frequently than the write thread, this time using read-write lock is more appropriate, read-write lock allows multiple read threads to access critical resources, read-write thread must mutually exclusive access The implementation of the read-write lock uses a mutex, so in the case of reading and writing the same number of times the use of read-write lock performance is not directly using a mutex lock high.

Condition variables: Provides a mechanism for notification between threads, when a condition is met, thread A can notify a thread that is blocked on the condition variable that the desired condition has been met, can b,b the blocking operation on the condition variable, and continue doing other things.

Semaphore: Provides a secure allocation of critical resources. If there are multiple critical resources, provide a way for the thread to securely allocate critical resources when multiple threads scramble for critical resources. If the number of critical resources is 1, it will degenerate into a lock.

Tokens: An advanced method of thread synchronization. It provides both the security of lock access critical resources, and the use of conditional variables so that the thread contention for critical resources is ordered.

7. Java's garbage collection mechanism



Java Technical interview Problem collation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.