Follow Finddreams Blog: http://blog.csdn.net/finddreams/article/details/44403041
Because Androd is programmed using the Java language, it is essential that we do Android development with a comprehensive grasp of the Java Foundation. In the course of the interview, we found that many companies issued written questions have a lot of knowledge points are Java, engaged in Android for a long time, some of the basic Java knowledge points are also quickly forgotten, today let us to review some Java Foundation, hope to be used in the interview;
1, the difference between overload and override. Can the overloaded method change the type of the return value?
The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload (overloading). The overloaded method is to change the type of the return value.
2. The difference between string and StringBuffer
The length of the string is immutable, and the length of the StringBuffer is variable. If you are frequently manipulating the contents of a string, especially if the content is to be modified, use StringBuffer, and if you need a string at the end, use the ToString () method of StringBuffer.
Stringbuder is unsafe String is safe
3, 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 the index data is fast and the data is inserted 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.
4. String "ABCDE" by writing a function does not let the third-party string, implement a string reverse, such as the string "ABCDE" into "EDCBA"
String src = "ABCDE";
String DST = new StringBuffer (src). Reverse (). toString ();
5. The difference between Collection and collections.
Collection is the ancestor interface of the collection class, and the main interface for inheriting it is set and list.
Collections is a helper class for collection classes that provides a series of static methods for searching, sorting, threading, and so on for various collections.
6, 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.
7. What is the difference between sleep () and wait ()?
1. The two methods come from different classes, namely, sleep comes from the thread class, and wait comes from the object class.
2. The most important thing is that the sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronization control block or method. Sleep does not sell system resources; Wait is the thread waiting for the pool to wait, to assign system resources, and other threads to consume the CPU. The general wait does not add a time limit, because if the wait thread runs out of resources, it is useless to wait for all threads in the Notify/notifyall wake-up waiting pool to be called by other threads before it enters the ready queue to wait for the OS to allocate system resources. Sleep (milliseconds) can be specified by time to wake it up automatically, if the time is less than the interrupt () force interrupt.
3.wait,notify and Notifyall can only be used in synchronous control methods or synchronization control blocks, and sleep can be used anywhere
4. Sleep needs to catch exceptions, and wait does not need
8. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used separately? An example is described.
If the data will be 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.
9, the difference between abstract class and interface (the difference between abstraction and interface)
Abstract can be used to modify abstractions, and for a class to have an abstract method, the class must be defined in abstract, which is an abstract class.
Interface-Modified classes, the methods are abstract methods, so when defining the interface, you can directly without those modifications, the system will be added by default. The fields inside the interface are public constants, which are the fields that are static final decorated.
10. Thread Wait,join,sleep,yield, notify,notifyall,synchronized, difference and contact
1). Sleep () method
Suspends execution of the currently executing thread for a specified period of time, but does not release the lock flag. It is not recommended. Sleep () causes the current thread to enter a blocking state that will not be executed for a specified time period.
2). Wait () method
Causes the current thread to wait before another thread calls the object's notify or Notifyall method. The thread releases the "lock flag" that it occupies, allowing other threads to seize the lock.
The waiting thread that wakes the current object lock uses the Notify or Notifyall method, and Waite () and notify () must be called in the synchronized function or synchronized block.
The yield method pauses the currently executing thread object. Yield () simply brings the current thread back to the executable state, so the execution
3) The yield () thread is likely to be executed immediately after it enters the executable state. Yield () only causes threads with the same priority or higher priority to have an opportunity to execute.
4). Join method
Waits for the thread to terminate. Wait for the thread that called the Join method to end, and then continue execution. such as: T.join ();//mainly used to wait for the T thread to run the end, without this sentence, main will be executed, resulting in unpredictable results.
11. Can interfaces inherit interfaces? Can an abstract class implement an (implements) interface? Does an abstract class inherit entity classes (concrete Class)?
Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, whether an abstract class can inherit an entity class, but only if the entity class must have an explicit constructor.
12, the abstract method can be static at the same time, whether it can be native at the same time, whether it can be synchronized at the same time?
13. Can I inherit the string class?
The string class is the final class and cannot be inherited.
14. Data types supported by Java switch:
Java supports five types of data
They were:
Byte, char, short, int, enumeration
These are the previous versions of JDK1.6. When JDK1.7, the string is added, so there are six of them relative to JDK1.7.
15, what is a singleton mode, please write one to:
The main purpose of the singleton mode is to ensure that only one instance of a class is present in a Java application.
The general singleton pattern usually has several forms:
The first form: Defines a class whose constructor is private, and it has a static private class variable that, when the class is initialized, gets a reference to it through a public getinstance method, and then calls the method in it.
publicclassprivateSingleton(){} //注意这是private 只供内部调用 privatestaticnew Singleton(); //这里提供了一个供外部访问本class的静态方法,可以直接访问 publicstaticgetInstance() { return instance; } }
第二种形式:
publicclass Singleton { privatestaticnull; publicstaticgetInstance() { //这个方法比上面有所改进,不用每次都进行生成对象,只是第一次 //使用时生成实例,提高了效率! if (instance==null) instance=new Singleton();return instance; }}
Other forms:
Defines a class whose constructor is private, and all methods are static.
The first form is generally considered to be more secure.
Android Development interview by--4. Common Java Foundation Pen questions