Do you know these Frequently Asked Questions?

Source: Internet
Author: User

Do you know these Frequently Asked Questions?

Recently, I saw some posts on the Internet, such as "Java traps and a basket" and "Interview Questions and Answers". After reading these posts, I feel that there are many things that we can easily ignore, to a certain extent, we can see the degree of knowledge about Java. However, these posts are messy, and I personally think the answers to some questions are inaccurate or completely incorrect. For this reason, I have summarized some java-related issues, added some issues, summarized them, and provided corresponding instructions. If something is wrong, correct it.

1. There is a return statement in try {}, so it followsCodeWill it be executed? When will it be executed before or after return?

Will be executed, before return.

2. What is the difference between error, exception, and runtimeexception?

Both error and exception are inherited from the throwable class, while runtimeexception is inherited from the exception class (this may be a bit unreasonable ).
Error indicates a very serious and unexpected exception, usually used Program No need to capture and process.
An exception is usually a design or implementation exception. It is an exception that the compiler can "perceive". Therefore, the program must capture and handle these exceptions.
Like error, runtimeexception does not need to be captured. Even if runtimeexception is throws on the method, the statements that call this method do not need to catch these exceptions.

3. What is the difference between & and?

& Is a bitwise operator (bitwise AND ). & Is a Boolean logical operator (equivalent to and ).

4. What is the difference between hashmap and hashtable?

In the same place: Map interfaces are implemented. hashtable inherits from the dectionary class (since JDK 1.0), and hashmap inherits from the abstractmap class (since JDK 1.2 ).
The main difference is that hashmap is non-synchronous while hashtable is synchronous. hashmap allows null keys and null values, but hashtable does not.
There is also a class that implements the sortedmap (MAP sub-interface) interface, called treemap. The biggest difference between treemap and hashmap is that treemap sorts keys while hashmap does not.
As for the statement that "hashmap is not synchronized, hashmap is faster than hashtable", I think it is not strict enough. The speed of hashmap and hashtable depends not only on synchronization or non-synchronization, but also on the implementation methods of different operations. On the Java performance portal, hashmap, hashtable, and treemap are tested. The related results are as follows:

Hashmap Hashtable Treemap
Fill 3 MS 2 MS MS
Iterate 111 MS 154 MS 594 MS
Remove MS MS 13 MS
(For details, see Name = News & file = article & SID = 18)

We can see that hashmap is slower than hashtable in fill operations. Hashmap is faster only when interate and remove are used.
In principle, hashmap should be given priority. The synchronization requirements can be increased through map M = collections. synchronizedmap (New hashmap.

5. What is the difference between collection and collections?

Collections is a tool class under java. util. It provides various static methods for set operations and has powerful functions.
Collection is an interface under java. util, and is the parent interface of various collection structures (list, set.

6. What is the difference between string S = new string ("hello") and string S = "hello?
If new is used, it clearly tells the JVM that the string object needs to be created, or not retrieved from the string pool. S = "hello" is first retrieved from the string pool, and is not newly created and put into the pool. For example:
String S1 = new string ("hello ");
String S2 = "hello ";
String S3 = "hello ";
Two string objects (with the value "hello") will be created: S1 points to one, S2 and S3 point to one, and S2 and S3 point to the object in the string pool.
Suggestion: Try to use the string S = "hello" format to improve efficiency.

7. What is the difference between the equals () method and =?
In general, equals compares whether the values of variables are equal, while = compares whether the memory addresses of variables are equal (that is, the same object ). For example, in the string defined above:
S1.equals (S2), s1.equals (S3), s2.equals (S3), and S2 = S3 are both true, while S1 = S2 and S1 = S3 are both false.
Suggestion: Try to use the equals () method, because we basically compare the variable value.

8. Short S1 = 1; S1 = S1 + 1; what is the error? Short S1 = 1; S1 + = 1; what is the error?

Short S1 = 1; S1 = S1 + 1; error: S1 is short type, S1 + 1 is int type, need to be converted to short type explicitly, change to S1 = (short) (S1 + 1 ).
Short S1 = 1; S1 + = 1; is correct.

9. Does the array have the length () method? Does string have the length () method?

The array does not have the length () method, but can be directly used with the Length attribute. String has the length () method.

10. Differences between overload and override. Can the overloaded method change the type of the returned value?

Override indicates that the subclass completely overwrites the parent class method.
Overload, overload, refers to a subclass or a method newly written in the same name as this class (the return value and parameters may be different ).
The overloaded method can change the type of the returned value, but not just the type of the returned value, but also the type or number of parameters.

11. Can constructor be overwritten?
Constructor cannot be inherited, so it cannot be overwritten, but can be overloaded ).

12. Can I inherit the string class?

The string class is a final class, so it cannot be inherited.

13. Do I use run () or start () to start a thread ()?

Start a thread using the START () method. Do not use run (), because run () is only the method of runnable interface. It is no different from the general method (run in the current thread ). The START () method is different. It starts a new thread and then executes the run () method on the new thread.

14. What is the difference between sleep () and wait?
The main differences are:
1) sleep () is the thread method, while wait () is the object method;
2) Wait () needs to be used in the synchronization method or block. When called, it releases the lock. Therefore, wait () suspends the thread, while sleep () does not (it will continue to occupy the CPU time ).
Suggestion: Try not to use sleep ().

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

If other methods are synchronized, they cannot be accessed. Otherwise, they can be accessed.

16. The two objects have the same value (X. Equals (y) = true), but different hash codes are available, right?

No. JDK has clearly stated that if the two objects are equal through the equals method, they must produce the same integer result when calling hashcode.

17. Does swtich work on byte, long, or string?

In switch (expr1), expr1 is an integer expression. Therefore, the parameters passed to the switch and case statements should be int, short, Char, or byte. Neither long nor string can work on swtich.

18. when an object is passed as a parameter to a method, this method can change the attributes of this object and return the changed result, so is it a value transfer or a reference transfer?

Is the value transfer. First, let's clarify an important concept: Unlike C ++, JAVA supports two parameter transfer methods (value transfer and address transfer). Java only supports parameter transfer. When an object instance is passed as a parameter to the called method, the method parameter is actually a copy of the reference value of the object. The object content can be changed in the called method by using this reference copy, but the change of the reference copy itself does not affect the external method, it is even more difficult to change the object (because the reference copy changes, it will not point to this object ). Therefore, the "and returned results after change" mentioned here is not accurate because the object is changed directly by using the object reference address obtained in the call method, rather than "return ". Therefore, Java cannot implement swap (INT, INT) methods like the well-known swap (INT) method in C/C ++, but this does not affect Java's use.

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.