Java interview questions: 10 errors

Source: Internet
Author: User
Tags finally block time zones stack trace volatile

1. System. exit (0) skips the execution of finally blocks.

The code is as follows: Copy code

System. setSecurityManager (new SecurityManager (){
@ Override public void checkExit (int status ){
Throw new ThreadDeath ();}
});

Try {
System. exit (0 );
}
Finally {
System. out. println ("In the finally block ");
 }

Why does this code output In the finally block? Why didn't stack trace information be printed? 2. String str = "Hello"; str is a String object different from C ++. The variables in Java are either basic types or references. Variables cannot be objects. This means an expression like this:

The code is as follows: Copy code
String str = "Hello ";
String text = "Bye ";
Str = text; // compare two references instead of content
Str = text; // assign the reference of text to str

In most cases, there is no big difference, but such writing is easy to get confused.

The code is as follows: Copy code
Final StringBuilder sb = new StringBuidler (); sb. append ("Hello ");//

This reference is of the final type, not the instance. Method (sb); // you can modify this instance by using the method, but this variable cannot be modified.
Java memory leakage, as the C ++ programmer understands, is defined in Wikipedia as "in computer science, if the program does not properly manage the memory allocation, memory leakage occurs. In object-oriented programming, if an object in the memory cannot be accessed in the code, this is memory leakage ." However, in Java, objects are always reachable, and objects without strong references will be cleared. The term memory leakage in Java means that there are objects in the memory that should not exist. Generally, some resources that are no longer in use are still stored in the collection.
Multi-threaded programming is really difficult if you have no experience. If you just throw a pile of code into a pile of threads for execution, the problem cannot be solved, but it can only be a mess. However, if you can allocate threads on demand and control interaction between threads, the problem becomes much simpler by using simple modes that some team members can understand. Of course, another challenge is that you have to keep everyone in the team following your rules :-)
I don't need to worry about the performance of different operations. Recently I heard that there is a problem. It involves the addition of integers, memory access, modulus, and output to the console. Although each of these operations is an order of magnitude slower than the previous one, this guy is trying to optimize the fastest operation, addition, some more expensive operations are also used to replace it. If you really want to optimize the performance, you 'd better replace those expensive operations with a cheap operation. If your bottleneck lies in the hardware, for example, reading a large number of files from the hard disk, modifying the software code is useless, because the problem is not at all.
Random numbers are random.

A specific random number is like a number in a certain pattern. I have mentioned this in this article. Many people do not believe that the number generated by the random number generator is not random.

Do not use floating-point numbers as far as possible because they produce random errors.
For the same operation, the floating point number will produce the same error each time. Errors are predictable and therefore controllable. If you know what you want to do and stick to some simple rules, such as rounding the results, there will be no more floating point errors than BigDecimal, in addition, it is more readable and faster than times (and fewer junk objects are generated at the same time ).

The time zone remains unchanged.

The reason for this misunderstanding is that the time zone is changing over time. This means that in the new era of Europe/London, it was rather than. Why? The reason is that London used daylight saving time between 1968 and 1971.

In the past few years, many time zones have also changed. Moscow used to be East 3 (GMT + 3) and now East 4 (GMT + 4) (starting from January 1, March 27, 2011 ). If you look at the time of January 1, 2010, you will find that it is East 3 rather than East 4.

Some other things may sound surprising to you:

Sweden has 30 days since January 1.

The first day of England in 1751 was in March 25, 11 days behind France.
After using the Gregorian calendar year in the United States, it goes back to the previous century, so that the dates recorded previously can be expressed in two calendar types (usually to be more precise, two dates will be provided at the same time ). For example, George Washington's birthday changed from February 11, 1731 to February 22, 1732.
When you read a non-volatile variable in the thread, you can finally read the updated value.
This problem occurred two times on StackOverflow a few days ago. In general, the JIT compiler will inline non-volatile fields not modified by this thread during code optimization. Once the code is compiled (you can see it through-XX: + PrintCompilation), you may never see this field after modifying it in another thread. Adding random synchronization blocks or print statements can delay the execution of the optimization, or disrupt the JIT compiler so that it does not execute the optimization.

Java interview questions are correct

Many Java interview questions are either outdated (not updated for more than 10 years, and out of touch with the current Java version) or misleading or even wrong. Unfortunately, these answers are sent everywhere without being checked.

I will refer to the answer above in Stackoverflow, because the answer here Peer review is better. In general, websites like rose india don't need to get on, and the quality of the above answers is far from satisfactory. If you like to learn the basics, you can check the spelling mistakes (class names and terminology) or incorrect comments in the previous article. One reason for these problems is that there is no effective feedback mechanism to correct these errors.

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