The following are all relatively advanced questions, and they are usually rarely asked during interviews because they may turn the interviewer out. But you can find some time to practice it yourself.
1. System.exit(0) will skip the execution of 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 is the stack trace information not printed?
2. String str = "Hello"; where str is a string object
Unlike C++, variables in Java are either basic types or references. A variable cannot be an object. This means an expression like this:
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 actually not much difference, but writing like this can easily cause confusion.
final StringBuilder sb = new StringBuilder(); sb.append("Hello"); // This reference is of final type, not this instance. method(sb); // This instance can be modified through methods, but this variable cannot be modified 3. Java memory leak is the same as C++ programmers understand
The definition of memory leak on Wikipedia is "In computer science, if a program does not manage memory allocation correctly, memory leaks will occur. 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 reachable, and those without strong references will be cleared. The term memory leak means in Java: there are objects that should not exist in memory, and usually some resources that are no longer used are still stored in the collection.
4. Multithreaded programming is difficult
If you have no experience, multi-threading programming is indeed difficult. If you just throw a bunch of code into a bunch of threads and execute it, then the problem cannot be solved at all, it will be a mess. But if you can perform thread allocation on demand, control interactions between threads, and use simple patterns that some members of the team can also understand, the problem becomes much simpler. Of course, there is another challenge that you have to make everyone in the team follow your rules
5. Don't care about the different performances between different operations
I recently heard that there is a problem, which 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 just wants to optimize the fastest operation, addition, and replace it with some more expensive operations. If you really want to optimize performance, you'd better replace those expensive operations with a cheap operation. If your bottleneck is in the hardware, for example, you have to read a large number of files from the hard disk, modifying the software's code is useless, because the problem is not at all.
6. Random numbers are random
A specific set of random numbers is like numbers of some pattern. I have already talked about this issue in this article. Many people do not believe that the numbers generated by random number generators are actually not random.
7. Floating points should be avoided as they will produce random errors
For the same operation, floating point numbers will produce the same error each time. Errors are predictable and therefore controllable. If you know what you are going to do and stick to some simple rules, such as rounding the results, then the floating point numbers will not make more mistakes than BigDecimal. In addition, it is more readable and more than a hundred times faster (and there are fewer garbage objects generated at the same time).
8. Time zone is eternal
The reason for this misunderstanding is that as time changes, the time zone is changing. This means that Europe/London was 1970/1/1 01:00 instead of 00:00. Why? Because London used daylight saving time in the two years from 1968 to 1971.
In the past few years, many time zones have also changed. Moscow used to be the East Third District (GMT+3), but now it is the East Fourth District (GMT+4) (starting from March 27, 2011). If you look at the time of 2010, you will find that it is East 3 and East 4.
There are some things you may sound surprised:
February of Sweden in 1721 has 30 days.
The first day in England in 1751 was March 25, which was 11 days behind France.
After the United States adopts the Gregorian calendar, it traces back hundreds of years, so that the dates originally recorded can be expressed in two calendars (usually two dates are provided at the same time for more precision). For example, George Washington's birthday changed from February 11, 1731 to February 22, 1732.
9. When you read a non-Volatile variable in a thread, you can finally read the value it updated.
This problem appeared twice on StackOverflow a few days ago. Generally speaking, when the JIT compiler optimizes the code, it will inline fields of non-volatile types that have not been modified to this thread. Once this code is compiled (you can see it with -XX:+PrintCompilation), it is likely that it will never be visible if you modify this field in another thread. Adding random synchronization blocks or print statements 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 correct
There are many Java interview questions that are either outdated (not updated for more than 10 years and are out of touch with the current Java version), or they are misleading, or they may even be wrong. Unfortunately, none of these answers were passed around without checking them.
I'll refer to Stackoverflow's answers above because the peer review here does a better job of reviewing the answers. In general, don’t go to websites like rose india, the answers above are of a ridiculous quality. If you like to get to the bottom of it, you can check out how many spelling errors (class names and professional terms) or wrong remarks are found in the above article. One reason for these problems is that there is no effective feedback mechanism to correct these errors.
I would like to recommend some Java interview questions:
The 50 most valuable Java interview questions are suitable for admission to Java programmers
10 classic Java main method interview questions
Discuss the ten most common interview questions in Java (super classic)
10 XML interview questions for Java programmers are released
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.