Summary of Java face questions

Source: Internet
Author: User

1. What is a JVM and how it works

2, the difference between equal and = = in Java

3. Differences between interfaces and abstract classes in Java

4. The difference between private, protected, public and default

5. Is string the most basic data type?

The basic data types are Boolean, Byte, char, int, short, long, float, double.

The Java.lang.String class is of the final type, so you cannot inherit the class or modify the class. In order to improve efficiency and save space, we should use the StringBuffer class

6. The difference between String, StringBuffer and StringBuilder

String: Immutable object, when changing a string object, is actually equivalent to generating a new string object, and then pointing the reference to the new string object, the original string object GC is recycled.
StringBuffer string variable (thread safe), suitable for multi-threaded programs, to ensure synchronization.

StringBuilder character string variable (non-thread safe), suitable for single-threaded programs, does not guarantee synchronization. Briefly, the performance difference between the String class and the Stringbuffer/stringbuilder class is that the string is an immutable object, so each time a change is made to the string class it is equivalent to generating a new string object and then referring to the The pin points to the new string object, so it is best not to use string to change the content of the strings, because each generation of the object will have an impact on the system performance, especially when there is no reference object in memory, the JVM's GC will start to work, the speed will be quite slow. If you use the Stringbuffer/stringbuilder class, the result will be different, and each result will operate on the Stringbuffer/stringbuilder object itself, instead of generating a new object and changing the object reference. Therefore, in general, it is recommended to use Stringbuffer/stringbuilder, especially if the string object changes frequently.

7. The difference between int and integer

The difference between int and integer is the difference between the basic data type and its wrapper class in large terms: int is the basic type, the value is stored directly, and the integer is the object, which points to the object with a reference. When you need to put something in the arraylist,hashmap, such as the int,double type is not put in, because the container is loaded with object, this is the need for these built-in type of outer covering class. Each of the built-in types in Java has a corresponding overlay class.

8, the difference between the run-time exception and the general exception

Java provides two main types of exceptions: Runtime exception and checked exception. The checked exception is the IO exception that we often encounter, and the SQL exception is the exception. For this exception, the JAVA compiler enforces that we must catch the exceptions that occur. So, in the face of such anomalies whether we like it or not, we can only write a lot of catch blocks to deal with possible exceptions. But another exception: Runtime exception, also known as runtime exception, we can not handle . 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. Runtime exceptions are subclasses of the Exception, and there are general exceptions that can be handled by Catch blocks. It's just that we're not dealing with him. That is, if you do not handle a run-time exception, then a run-time exception occurs, either the thread aborts or the main program terminates.

9, say Arraylist,vector, LinkedList storage performance and Characteristics
Both ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement, so finding data is fast and inserting data slowly, Vector because of the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList, and LinkedList using a doubly linked list for storage, index data by ordinal need to be forward or backward traversal, but when inserting data only need to record the item before and after items can be , so the insertion speed is faster
In addition: HashMap is not synchronized, and Hashtable is synchronous, that is, thread-safe
10. servlet Related Issues
Load: Can be loaded when a servlet container, such as Tomcat, can be loaded at the time of client access, and the time to load is determined by the configuration item in the Web. XML configuration file.
Initialize: Call the Init method
Servicing: Invoking service methods, such as Doget, Dopost methods
Destroy: Call the Destory method
The servlet is a singleton pattern, and after Tomcat launches a servlet instance, it requests services for all clients that access the servlet, so be aware of thread safety when writing a servlet, cannot define variable member variables in a class, you should define member variables in methods such as Doget
11. The difference between a GET request for HTTP and a POST request
Get requests are typically used to fetch data from the server, typically without a package, and post requests typically send data to the server, and the data is generally placed in the request package body. Get requests can also send data to the server, but the data should be placed in the URL, with the & split key-value pairs sent in a way that generally can only send some low-security and small data
12, final, finally, finalize the difference.
Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.
Finally is part of the exception-handling statement structure, which indicates that it is always executed.
Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, and can override this method to provide garbage collection of other resource recycles, such as closing the file.
Multithreading mechanism
13, Real threading method, what is the difference between the present

inherit the thread and implement the Runnable interface. The startup method is different. Thread1 inheritance, Thread2 implements the Runnable interface, the start of a Thread1 thread can use new Thread1 (). Start (), and the start of the Thread2 thread is new Thread2 ()). Start ().

14. Can I start a thread using the Run method?
Start a thread should use the Start method, the thread's Run method can be called directly, but does not start a new thread, just called the Run method in the original thread.
15. What is the difference between sleep and wait methods, with time parameters?
Sleep () is a static method of a thread that suspends execution for a period of time by the current thread (the thread that calls the method), allowing other threads to continue executing, but it does not release the object lock. That is, if you have a synchronized synchronization block, other threads still cannot access the shared data. The wait () method suspends the current thread and frees the object lock flag so that other threads can enter the synchronized data block and the current thread is placed in the object waiting pool.
16. Wait () and notify (), Notifyall ( )
These three methods are used to coordinate the access of multiple threads to shared data, so you must use these three methods within a synchronized statement block. Synchronized this keyword is used to protect shared data and prevent other threads from accessing shared data.
The wait () method suspends the current thread and frees the object lock flag so that other threads can enter the synchronized data block and the current thread is placed in the object waiting pool. When the Notify () method is called, an arbitrary thread is removed from the object's wait pool and placed in the lock flag waiting pool, and only the lock flag waits for the thread in the pool to acquire the lock flag, and notify () does not work if there are no threads in the lock flag waiting pool. Notifyall () removes all the threads waiting for that object from the object waiting pool and puts it in the lock flag waiting pool.
17. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used separately? An example is described.
Answer: If the data is shared between threads. For example, the data being written may be read by another thread later, or the data being read may have been written by another thread, then the data is shared and must be accessed synchronously. When an application calls a method that takes a long time to execute on an object and does not want the program to wait for the method to be returned, it should use asynchronous programming, which is often more efficient in many cases with asynchronous approaches.
18. When a thread enters an synchronized method of an object, does the other thread have access to other methods of this object?
No, an synchronized method of an object can only be accessed by one thread.
19. Please say the thread synchronization method you know.
Wait (): causes a thread to be in a wait state and releases the lock of the object it holds.
Sleep (): causes a running thread to be asleep and is a static method
Notify (): Wakes up a waiting thread, noting that when this method is called, it does not actually wake up a waiting state thread, but is determined by the JVM to wake up which thread, and not by priority.
Notifyall (): Wakes all the threads that are in the waiting state, noting that they do not give all the wake-up threads an object lock, but instead let them compete.
20, briefly describe the similarities and differences between synchronized and Java.util.concurrent.locks.Lock?
Main similarities: Lock can complete all functions implemented by synchronized
Main differences: Lock has more precise line semantics and better performance than synchronized. The synchronized automatically releases the lock, and lock must require the programmer to release it manually, and must be released in the finally clause.
21. What happens when I use synchronization on a static method?
When a static method is synchronized, the class object is obtained, so when a thread enters a synchronous static method, the thread monitor acquires the object lock of the class itself, and other threads cannot enter any static synchronization methods of the class. It is not like an instance method, because multiple threads can access different instance synchronization instance methods at the same time.
22. What is serialization? Why do serialization and deserialization

23. Java's three big frameworks? What is the MVC pattern

Structs, Spring, Hibernate

Summary of Java face questions

Related Article

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: 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.