Android development interview: 4. Common basic Java questions, androidjava

Source: Internet
Author: User

Android development interview: 4. Common basic Java questions, androidjava

Follow finddreams blog: http://blog.csdn.net/finddreams/article/details/44403041
Because Android is programmed using the Java language, it is necessary for us to fully master the Java foundation for Android development. During the interview, we found that many of the company's pen questions were written in Java, and some knowledge points on the basis of Java have been forgotten for a long time, today, let's review some Java basics, hoping to be used in the interview;

1. Differences between Overload and Override. Can the Overloaded method change the type of the returned value?
Overriding and Overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and Overloading is a manifestation of the polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called Overloading ). The Overloaded method can change the type of the returned value.

2. Differences between String and StringBuffer
The length of String is unchangeable, and the length of StringBuffer is variable. If you often operate on the content in the String, especially when the content is to be modified, use StringBuffer. If the String is needed at last, use the toString () method of StringBuffer.
StringBuder is insecure. String is safe.

3. Specify the storage performance and features of ArrayList, Vector, and sorted list.
Both ArrayList and Vector use arrays to store data. The number of elements in the array is greater than that in the actual data storage to add and insert elements. They allow the element to be indexed by serial number directly, however, inserting elements involves memory operations such as array element movement, so index data is fast and data insertion is slow. Because Vector uses the synchronized Method (thread-safe), its performance is generally inferior to that of ArrayList, the sorted list uses a two-way linked list for storage. Data indexed by serial number needs to be traversed in the forward or backward direction. However, when inserting data, you only need to record the items before and after this item, so the insertion speed is fast.

4. the string "abcde" is written into a function to prevent the call of a third-party string and implement a reverse string. For example, the string "abcde" is changed to "edcba"
String src = "abcde ";
String dst = new StringBuffer (src). reverse (). toString ();

5. Differences between Collection and Collections.
Collection is the upper-level interface of the Collection class. Its inherited interfaces include Set and List.
Collections is a help class for collection classes. It provides a series of static methods for searching, sorting, thread security, and other operations on various sets.

6. Differences between final, finally, and finalize.
Final is used to declare attributes. Methods and classes indicate that attributes are unchangeable, methods cannot be overwritten, and classes cannot be inherited.
Finally is a part of the structure of the exception handling statement, indicating that it is always executed.
Finalize is a method of the Object class. This method is called when the garbage collector is executed. It can overwrite this method to collect other resources during garbage collection, for example, close a file.

7. What is the difference between sleep () and wait?
1. The two methods come from different classes: sleep from the Thread class and wait from the Object class.
2. The most important thing is that the sleep method does not release the lock, and the wait method releases the lock, so that other threads can use synchronous control blocks or methods. Sleep does not assign system resources; wait enters the thread wait pool to wait, and transfers system resources. Other threads can occupy the CPU. Generally, wait does not apply a time limit because it is useless if the Running Resources of the wait thread are insufficient. wait for other threads to call y/yyall to wake up all threads in the waiting pool, wait until the OS allocates system resources. Sleep (milliseconds) can automatically wake up with a specified time. If the time is not enough, interrupt () can only be called to force the interruption.
3. wait, policy, and policyall can only be used in synchronous control methods or synchronization control blocks, while sleep can be used anywhere.
4. Sleep Needs to capture exceptions, while wait does not.

8. What are the similarities and differences between synchronization and Asynchronization? Under what circumstances should they be used separately? Examples.
If data is shared among threads. For example, if the data being written may be read by another thread or the data being read may have been written by another thread, 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 return of the method, asynchronous programming should be used, in many cases, adopting asynchronous channels is often more efficient.

9. Differences between abstract classes and interfaces (difference between abstract and interface)
Abstract can modify abstract methods. A Class must use abstract to define the class as long as there is an abstract method.
Interfaces are used to modify classes. The methods in these classes are abstract methods. Therefore, when defining interfaces, you can directly remove the modifiers. The system will add them by default. All the fields in the interface are public constants, that is, the fields modified by public static final.

10. Differences and connection between wait, join, sleep, yield, y, policyall, and synchronized in the thread
1). sleep () method
Pause the execution of the currently executing thread within the specified time, but do not release the "Lock mark ". It is not recommended. Sleep () causes the current thread to enter the blocking state and will not be executed within the specified time.
2). wait () method
The current thread waits until other threads call the notify or yyall methods of the object. The thread will release the lock flag it occupies, so that other threads have the opportunity to seize the lock.
The wait thread that wakes up the current object lock uses the notify or notifyAll method. waite () and notify () must be called in the synchronized function or synchronized block.
The yield method pauses the currently executed thread object. Yield () only enables the current thread to return to the executable state, so the execution
3) The yield () thread may be executed immediately after it enters the executable state. Yield () can only give threads with the same or higher priority a chance to execute.
4) join method
Wait for the thread to terminate. Wait until the thread that calls the join method ends and continue the execution. For example, t. join (); // It is mainly used to wait for the end of t thread running. If this sentence is not provided, main will be executed, leading to unpredictable results.

11. Can an interface inherit an interface? Can an abstract class implement the (implements) interface? Can an abstract class inherit a concrete class )?
Interfaces can inherit interfaces. Abstract classes can be implemented (implements) interfaces, and whether abstract classes can inherit object classes, provided that object classes must have clear constructors.

12. can abstract methods be both static, native, and synchronized?

13. Can I inherit the String class?
The String class is a final class, so it cannot be inherited.

14. Data Types supported by java switch:
Java supports five Data Types
They are:
Byte, char, short, int, enumeration
The above is a version earlier than JDK1.6. In JDK1.7, the String is added, so there are six types compared with JDK1.7.

15. For the singleton mode, write one:
The Singleton mode ensures that only one instance of a Class exists in a Java application.
The Singleton mode generally has several forms:
The first form: defines a class. Its constructor is private. It has a static private class variable. When the class is initialized, use a public getInstance method to obtain its reference, and then call the method.

Public class Singleton {private Singleton () {}// note that this is private only for internal calls private static Singleton instance = new Singleton (); // here is a static method for external access to this class. You can directly access public static Singleton getInstance () {return instance ;}}
Second form:
Public class Singleton {private static Singleton instance = null; public static synchronized Singleton getInstance () {// This method is better than above, so you do not need to generate objects every time, it is only the first time that an instance is generated/used, improving the efficiency! If (instance = null) instance = new Singleton (); return instance ;}}

Other forms:
Defines a class. Its constructor is private and all methods are static.
It is generally considered that the first form is more secure.

16. Common Java design patterns? Describe the factory model.
23 Design Patterns in Java:
Factory (Factory mode), Builder (construction mode), Factory Method (Factory Method mode ),
Prototype (original model mode), Singleton (Singleton mode), Facade (Facade mode ),
Adapter (Adapter mode), Bridge (Bridge Mode), Composite (merging mode ),
Decorator, Flyweight, Proxy ),
Command (Command mode), Interpreter (Interpreter mode), Visitor (Visitor mode ),
Iterator, Mediator, Memento ),
Observer (Observer mode), State (State mode), Strategy (Policy mode ),
Template Method (Template Method mode), Chain Of Responsibleity (responsibility Chain mode)
Factory mode: The factory mode is a frequently used mode. Classes implemented based on the factory mode can generate instances of a class in a group based on the provided data,
Generally, this group of classes has a common abstract parent class that implements the same method, but these methods perform different operations on different data.
First, you need to define a base class. The subclass of this class implements the methods in the base class through different methods. Then a factory class needs to be defined. The factory class can be defined according to the conditions.
Generate different subclass instances. After obtaining the subclass instance, developers can call the methods in the base class without having to consider which subclass instance is returned.

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.