search
HomeJavajavaTutorial10 lies about Java

10 lies about Java

Jan 18, 2017 pm 03:21 PM
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)!


Statement
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
How does the JVM manage garbage collection across different platforms?How does the JVM manage garbage collection across different platforms?Apr 28, 2025 am 12:23 AM

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Why can Java code run on different operating systems without modification?Why can Java code run on different operating systems without modification?Apr 28, 2025 am 12:14 AM

Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.

Describe the process of compiling and executing a Java program, highlighting platform independence.Describe the process of compiling and executing a Java program, highlighting platform independence.Apr 28, 2025 am 12:08 AM

The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.

How does the underlying hardware architecture affect Java's performance?How does the underlying hardware architecture affect Java's performance?Apr 28, 2025 am 12:05 AM

Java performance is closely related to hardware architecture, and understanding this relationship can significantly improve programming capabilities. 1) The JVM converts Java bytecode into machine instructions through JIT compilation, which is affected by the CPU architecture. 2) Memory management and garbage collection are affected by RAM and memory bus speed. 3) Cache and branch prediction optimize Java code execution. 4) Multi-threading and parallel processing improve performance on multi-core systems.

Explain why native libraries can break Java's platform independence.Explain why native libraries can break Java's platform independence.Apr 28, 2025 am 12:02 AM

Using native libraries will destroy Java's platform independence, because these libraries need to be compiled separately for each operating system. 1) The native library interacts with Java through JNI, providing functions that cannot be directly implemented by Java. 2) Using native libraries increases project complexity and requires managing library files for different platforms. 3) Although native libraries can improve performance, they should be used with caution and conducted cross-platform testing.

How does the JVM handle differences in operating system APIs?How does the JVM handle differences in operating system APIs?Apr 27, 2025 am 12:18 AM

JVM handles operating system API differences through JavaNativeInterface (JNI) and Java standard library: 1. JNI allows Java code to call local code and directly interact with the operating system API. 2. The Java standard library provides a unified API, which is internally mapped to different operating system APIs to ensure that the code runs across platforms.

How does the modularity introduced in Java 9 impact platform independence?How does the modularity introduced in Java 9 impact platform independence?Apr 27, 2025 am 12:15 AM

modularitydoesnotdirectlyaffectJava'splatformindependence.Java'splatformindependenceismaintainedbytheJVM,butmodularityinfluencesapplicationstructureandmanagement,indirectlyimpactingplatformindependence.1)Deploymentanddistributionbecomemoreefficientwi

What is bytecode, and how does it relate to Java's platform independence?What is bytecode, and how does it relate to Java's platform independence?Apr 27, 2025 am 12:06 AM

BytecodeinJavaistheintermediaterepresentationthatenablesplatformindependence.1)Javacodeiscompiledintobytecodestoredin.classfiles.2)TheJVMinterpretsorcompilesthisbytecodeintomachinecodeatruntime,allowingthesamebytecodetorunonanydevicewithaJVM,thusfulf

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.