Do you understand these frequently asked questions?

xiaoxiao2021-03-06  114

Do you understand these frequently asked questions?

Recently, I saw some posts like "Java Trap", "Interview Quest Sets and Solution". After reading, I feel that there is a lot of things that we are easily ignored, and you can see to Java to a certain extent. Learn degree. But these posts are more chaotic, and some questions about answers I personally think that it is not accurate enough or is completely wrong. To this end, I roughly organized some questions related to Java, and added some problems, summarizes and gives the corresponding description. If there is something wrong, please correct it. 1, Try {} has a return statement, then the code in finally {} after this try is not executed, when is executed, before returnome or after it will execute, execute before return. 2, Error, Exception and RuntimeException? Error and Exception are inherited from the Throwable class, and RuntimeException inherits from the Exception class (this may be a bit unreasonable). Error represents very serious, unpredictable abnormal conditions, usually the application does not need to capture and process. Exception is usually an exception for design or implementation. It is an exception that the compiler can "perceive", so the program is required to capture and process these exceptions. RuntimeException is the same as Error, but also does not need to be captured, and even if the throws in the method is RuntimeException, the statement that calls the method does not need to catch these exceptions. 3, & and &&'s difference? & Is the bit operator (bit). && is a Boolean logic operator (equivalent to and mean). 4, the difference between Hashmap and HashTable? The same place: all implementation of the MAP interface, hashtable inherits from the Dectionary class (starting from JDK 1.0), HashMap inherits from the AbstractMap class (start from JDK 1.2). The main difference: HashMap is non-synchronous and hashtable is synchronized, and the HashMap allows the NULL key and null values ​​and HashTable is not allowed. There is also a class that implements the sortedmap interface, called TreeMap. The maximum difference between Treemap and HashMap is: TreeMap is sorted by KEY, while HashMap is not sorted. As for "because hashmap is not synchronized, Hashmap is better than HashTable", I think it is not strict. HashMap and HashTable is not only dependent on synchronization or non-synchronization, and the implementation method of different operations is also affecting factors. The Java Performance Portal has been tested on Hashmap, Hashtable and Treemap on the website. The relevant results are as follows:

HashmaphashtableTreemapFill3 MS2 MS9 Msiterate 111 MS154 MS594 MSREMOVE5 MS6 MS13 MS

(See details

http://www.java-performance-portal.org/modules.php?name=news&file=article&sid=18

)

It can be seen that HashMap is slower than HashTable on Fill. Hashmap is faster only when Interage and Remove are in Interate and Remove.

In principle, it should be given priority to use HashMap, its synchronization requirements, can be increased by: map m = collections.synchronizedmap (new hashmap (...)).

5. COLLECTION and COLLECTIONS Differences? Collectes are tool classes under java.util, providing a variety of static methods related to collection operations, and is very powerful.

Collection is the interface under java.util, which is a parent interface of a variety of set structures (List, SET).

6, String S = New String ("Hello") and String S = "Hello" What is the difference?

With new words, I will clearly tell the JVM of this string to create new creation, nor to the string pool. And S = "Hello" will be first to the string pool, and there is no new creation and put it in the pool. such as:

String S1 = New String ("Hello");

String S2 = "Hello";

String s3 = "hello";

The two String objects will be created (the value "Hello"): S1 points to one, S2 and S3 together point to one, and the objects indicted by S2 and S3 are placed in the string pool.

Recommendation: Try to use String S = "Hello" form to improve efficiency.

7, equals () method is different from ==?

Generally, the value of equals compares variables, and == is whether the memory address of the comparison variable is equal (ie the same object). For example, the string defined above:

S1.Equals (S2), S1.EQUALS (S3), S2.Equals (S3), and S2 == S3 are True, while S1 == S2, S1 == S3 are false.

Recommendation: Try to use the equals () method because we basically compare variables.

8, Short S1 = 1; S1 = S1 1; What is wrong? Short S1 = 1; S1 = 1; What is wrong?

Short S1 = 1; S1 = S1 1; is wrong, S1 is the short type, S1 1 is an INT type, which requires explicit conversion to a SHORT type, that is, modified to S1 = (S1 1).

Short S1 = 1; S1 = 1; it is correct.

9. Is there any length () method? STRING Is there any length () method?

The array does not have a length () method, but there is a length attribute that can be used directly. String has a length () method.

10, the difference between overload and Override. Can OVERLOADED methods change the type of return value?

Override, overwriting, fingers overwriting over the parent class method.

OverLoad, overload, fingers, or this class newly written the same named method (return value, parameter may be different).

OverLoaded method can change the type of return value, but it is only just that the type of return value is changed, and the type or number of parameters is required.

11. Is the constructor CONSTRUCTOR can be Override?

The constructor constructor cannot be inherited, so it cannot be overriddled, but it can be overloaded.

12. Can you inherit the String class?

The String class is that the final class cannot be inherited.

13. Start a thread is Run () or start ()?

Start a thread with the start () method. Do not use Run (), because run () is just the interface method of runnable, in addition to this, there is nothing different from the general method (running in the current thread). START () is not the same, it will start a new thread and let the run () method execute on the new thread. 14. What is the difference between SLEEP () and WAIT ()?

The main difference is:

1) SLEEP () is the method of thread, and Wait () is an object of object;

2) Wait () needs to be used in the synchronization method or block, it will release the lock. So Wait () will hang the thread, and Sleep () will not (will continue to occupy the CPU time).

Recommendation: Try not to use Sleep ().

15. After a thread enters an SYNCHRONIZED method of an object, can other threads can enter other methods of this object?

If other methods are also synchronized, they cannot be accessed, otherwise you can access.

16, the same value is the same (x.equals (y) == true, but there is a different Hash code, this sentence is wrong?

wrong. In JDK, it has been clearly stated that if the two objects determine from the Equals method, they must generate the same integer results when they call HashCode.

17. Can SWTICH be active on byte, long or string?

Switch (expr1), expr1 is an integer expression. Therefore, the parameters passing to the Switch and the CASE statement should be int, short, char or byte. LONG and STRING do not work on SWTICH.

18. After an object is passed to a method as a parameter, this method can change the properties of this object and return the changed result, then here is the value delivery or reference delivery?

It is a value transfer. First, clear an important concept: Java is unlike C supports two parameter transfer modes (pass value, attern), Java only supports the parameter transmission. When an object instance is passed as a parameter to the call to the call, the parameters of the method are actually a copy of the value of the reference to the object. The content of the object can be changed by this reference copy in the called method, but the change in this reference copy itself does not affect the method, it is more impossible to change the object (because the reference copy changes, no longer point to the object. ). So what is said here "and can return change" is not accurate, and the change is because the object reference address used in the modified method changes the object, not "return". Therefore, the famous SWAP (Int, int) method in C / C , Java is no way, but this does not affect the use of Java.

转载请注明原文地址:https://www.9cbs.com/read-103581.html

New Post(0)