10 lies about Java
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"); }
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
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); // 可以通过方法来修改这个实例,不过这个变量是无法修改的
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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
