Home > Java > javaTutorial > 10 lies about Java

10 lies about Java

黄舟
Release: 2017-01-18 15:21:38
Original
1397 people have browsed it

Java Programming Language

Java is an object-oriented programming language that can write cross-platform application software. It is a Java programming language and Java platform launched by Sun Microsystems in May 1995. (That is, the general name of JavaEE(j2ee), JavaME(j2me), JavaSE(j2se)).


The following questions are considered relatively advanced questions and are rarely asked in interviews because they may turn away interviewees. But you can find time to practice it yourself.

1. System.exit(0) will skip 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");
    }
Copy after login

Why does this code output In the finally block? Why is the stack trace not printed?

2. String str = "Hello"; where str is a string object. Unlike C++, variables in Java are either basic types or references. Variables cannot be objects. This means that expressions like this:

String str = "Hello";
    String text = "Bye";
    str == text; // 比较两个引用,而不是内容
    str = text; // 把text的引用赋值给str
Copy after login

In most cases there is not much difference, but it can cause confusion when written this way.

final StringBuilder sb = new StringBuidler();
    sb.append("Hello"); // 这个引用是final类型的,而不是这个实例。
    method(sb); // 可以通过方法来修改这个实例,不过这个变量是无法修改的
Copy after login

3. Java memory leaks are the same as C++ programmers understand

The definition of memory leaks on Wikipedia is "In computer science, if a program does not properly manage memory allocation , a memory leak will occur. In object-oriented programming, if an object in memory cannot be accessed in the code, this is a memory leak. "But in Java, objects are always reachable, those without strong references. The objects will be cleared. The term memory leak in Java means that there are objects in memory that should not exist, usually some resources that are no longer used but are still stored in the collection.

4. Multi-threaded programming is difficult

If you have no experience, multi-threaded programming is indeed difficult. If you just throw a bunch of code into a bunch of threads for execution, there will be no way to solve the problem and it will just be a mess. But if you can allocate threads on demand, control the interaction between threads, and use some simple patterns that members of the team can understand, the problem becomes much simpler. Of course, another challenge is that you have to make everyone in the team follow your rules:-)

5. Don’t worry about the performance differences between different operations

I recently heard that there is The problem involves adding integers, memory access, modulo, and output to the console. Although each of these operations is an order of magnitude slower than the previous one, this guy just wanted 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 need to read a large number of files from the hard disk and modify the software code. It's useless, because the problem doesn't lie there at all.

6. Random numbers are random

A specific set of random numbers is like a certain pattern of numbers. I have already talked about this issue in this article. Many people don't believe that the numbers generated by random number generators are actually non-random.

7. You should try to avoid using floating point numbers because they will produce random errors

For the same operation, floating point numbers will produce the same error every time. Errors are predictable and therefore controllable. If you know what you're trying to do and stick to some simple rules, like rounding the result, you won't get any more wrong with a floating point number than with a BigDecimal, and besides that it's more readable It is more robust and more than a hundred times faster (and generates fewer garbage objects at the same time).

8. The time zone is eternal

The reason for this misunderstanding is that as time changes, 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 between 1968 and 1971.

In the past few years, many time zones have also changed. Moscow used to be the East 3rd District (GMT+3), but now it is the East 4th District (GMT+4) (starting March 27, 2011). If you look at the time in 2010, you will find that it is the East 3rd District instead of the East 4th District.

There is something else that may sound surprising to you:

February in Sweden in 1721 had 30 days.

The first day in England in 1751 was March 25, which was 11 days behind France.

After the United States adopted the Gregorian calendar, it went back hundreds of years so that the dates originally recorded could be represented by two calendars (usually both dates were provided for greater accuracy). 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 eventually read its updated value.

This question appeared twice on StackOverflow a few days ago. Generally speaking, when the JIT compiler optimizes the code, it will inline the non-volatile fields that have not been modified by this thread. Once this code is compiled (you can see it through -XX:+PrintCompilation), the modifications you make to this field in another thread will probably never be seen. Adding random synchronization blocks or print statements can delay the execution of this optimization, or confuse the JIT compiler so that it does not perform this optimization.

10. Java interview questions are all correct

There are many Java interview questions that are either outdated (have not been updated in more than 10 years, and are out of touch with the current Java version), or are misleading Everyone's, maybe even wrong. Unfortunately these answers are passed around without being checked.

I will refer to the answers above on Stackoverflow, because the answers here are better peer-reviewed. In general, don't use websites like rose india. The quality of the answers above is ridiculously poor. If you like to dig deeper, you can take a look at how many spelling errors (class names and professional terms) or wrong statements there are in the above article. One reason for these problems is that there is no effective feedback mechanism to correct these errors.

The above is the content of 10 lies about Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template