about the 10 Lies of Java
interview always encounter a variety of wonderful problems, small in the brother even found on the website The following are some of the more advanced questions that are rarely asked during the interview because they may shut out the interviewer. But you can find a time to practice it yourself.
1. System.exit (0) skips the execution of the finally block
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 isn't the stack trace information printed?
2. String str = "Hello", where Str is a string object
with Unlike C + +, variables in Java are either the underlying type or the reference. A variable cannot be an object. This means an expression like this:
String str = "Hello";
String Text = "Bye";
str = = text; Compare two references, not content
str = text; Assigning a reference to the text to Str
In most cases there is not much difference, but it is easy to confuse.
Final StringBuilder sb = new Stringbuidler ();
Sb.append ("Hello"); This reference is of the final type, not the instance.
Method (SB); This instance can be modified by means of a method, but this variable cannot be modified
3.Java memory leaks are the same as C + + programmers understand
memory leaks are defined on Wikipedia as "in Computer science, a memory leak occurs if the program does not properly manage memory allocations." In object-oriented programming, if an object in memory cannot be accessed in code, this is a memory leak. "However, in Java, objects are always accessible, and objects that are not strongly referenced are erased." Memory leak The term in Java means that there are objects in memory that should not exist, and often some resources that are no longer used are still stored in the collection.
4. Multithreaded programming is difficult
multithreaded programming is really hard if you have no experience. If you just throw a bunch of code into a bunch of threads to do it, then the problem doesn't work out, it's only a mess. But if you can do thread-on-demand, control the interaction between threads, and use simple patterns that some members of your team can understand, the problem becomes much simpler. The challenge, of course, is that you have to let everyone in the team follow your rules.
5. Do not care about the difference in performance between different operations
recently heard of a problem, it involves the addition of integers, memory access, modulo, and output to the console. Although in these operations, each one is slower than the previous one order of magnitude, but this buddy is to optimize the fastest operation here, addition, but also with some more expensive operation to replace it. If you really want to optimize performance, you'd better use a cheap operation to replace those expensive operations, if your bottleneck in the hardware this piece, for example, to read a large number of files from the hard disk, modify the software code is useless, because the problem is not here at all.
6. Random numbers are random
A specific set of random numbers is like a number in a pattern. I have already mentioned this question in this article. Many people do not believe that the numbers generated by the random number generator are actually not random.
7. You should avoid using floating-point numbers as they produce random errors
for the same operation, the floating-point number produces the same error every time. Errors are predictable and therefore controllable. If you know what you're going to do, and stick with some simple rules, like rounding the results, then there's no more floating-point error than BigDecimal, but it's more readable and more efficient (and less garbage objects are generated).
8. The time zone is immutable
the reason for this misunderstanding is that, over time, the time zone is changing. This means that Europe /London in the new era is 1970/1/1 01:00 instead of 00:00, why? Because London used daylight saving time during the two years from 1968 to 1971. In the past few years, there have been many time zones have also changed. Moscow was formerly the East Third District (gmt+3) and is now the Eastern District (gmt+4) (starting March 27, 2011). If you look at the next 2010 years, you will find that it is in the East Third District and not the East four.
There are some things you might sound surprised at:
• Sweden in 1721 has 30 days in February.
• The first day of England in 1751 was March 25, 11 days worse than France.
after the United States adopted the Gregorian calendar, it dates back hundreds of years, so that those dates that were originally recorded can be represented in two calendars (usually to provide two dates at a time). George Washington's birthday, for example , changed from February 1731 11 to February 1732 22.
9. When you read a non-volatile variable in the thread, you can eventually read the value it updated.
a few days ago this problem was There were two stackoverflow on the back. In general, the JIT compiler optimizes code to inline non-volatile types of fields that the thread does not modify. Once the code has been compiled (you can see it through-xx:+printcompilation), you'll probably never see it in another thread's modification of the field. Adding a random synchronization block or a print statement can delay the execution of this optimization, or disrupt the JIT compiler so that it does not perform this optimization.
10.Java interview questions are all right.
there are many Java-side questions are either outdated (not updated for more than 10 years, and now have a disjointed version of Java), or they are misleading, and may even be wrong. Unfortunately, none of these answers has been checked and sent everywhere.
I will refer to StackOverflow the answers above, because the answers here are better than the peer review. On the whole, sites like Rose India are not on the list, and the answers are ridiculously poor in quality. If you like root planing, you can see how many spelling mistakes (class names and professional terms) or wrong comments are in the above article. One reason for these problems is that there is no effective feedback mechanism to correct these errors.
If there is a mistake in the small part of the place, you are welcome to Brother even forum, we exchange learning
10 Lies about Java