10 Lies about Java

Source: Internet
Author: User
Tags finally block time zones stack trace


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.

System.exit (0) skips the execution of a finally block

System.setsecuritymanager (NewSecurityManager () {@Override Public voidCheckexit (intStatus) {Throw NewThreaddeath (); }    });Try{System.exit (0); }finally{System. out. println ("in 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 unlike C + +, the variables in Java are either the underlying type or the reference. A variable cannot be an object. This means an expression like this:

"Hello";    "Bye";    //Compare two references instead of content    //Assign the text reference to STR

string str = " Hello " ;

string text =   "Bye"  ;

str == text ;  //compares two references instead of 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 New Stringbuidler ();    Sb.append ("Hello"//This reference is the final type, not the instance.)     //You can modify this instance by means of the method, but this variable cannot be modified.

final stringbuilder sb = new stringbuidler  ( )  ;

sb . append  (   "Hello"  )  ; //This reference is the final type, not the instance.

method  ( SB  )  ; //can modify this instance by means of the method, but this variable is not modifiable

      java's memory leak is the same as a C + + Programmer's understanding that 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.
      multithreaded programming is difficult if you have no experience, multithreaded programming is really difficult. 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 on the team follow your rule:-)
    • Don't worry about the different performance of different operations recently heard a question, 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.
    • 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.

    • You should try to avoid using floating-point numbers because 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).

    • 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:

    • 1721 of Sweden in February there are 30 days.
    • The first day of 1751 in England was March 25, which was 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.
    • When you read a non-volatile variable in the thread, you can eventually read the value it updated.

A few days ago this problem appeared on the StackOverflow two 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.

    • The Java interview question is all right.

There are many Java face questions that are either outdated (not updated for more than 10 years, and now have a disjointed version of Java), either misleading or even wrong. Unfortunately, none of these answers has been checked and sent everywhere.

I will refer to StackOverflow above answer, because here the answer peer review do better. 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.

Article from: http://it.deepinmind.com/java/2014/05/10/common-java-myths.html


10 Lies about Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.