"Thinking in Java" Reading notes (i)

Source: Internet
Author: User

recently began to read Tij, good acute than bad writing, so or record a bit, convenient for later inspection.


first, various initialization problems:
        problem with method overloading:

The overloads of a method are distinguished by the arguments passed into the method, not by the return value. such as f (), if the return value, it is easy to create ambiguity.

method, the type of the passed-in parameter is promoted or lowered; for example, if there is a method to accept the int type, if a char type is passed in, the call to the method is automatically promoted.

When it comes to overloading problems with mutable parameter methods, there are special cases:

        static void F (float F, Character ... c) {System.out.println ("First");} static void F (Character ... c) {//If this is not preceded by a char C1, it will cause a compilation error System.out.println ("Second");}

F ("1", "2");//A compilation error occurs


The second method should be written in the following way:


static void F (char C1, Character ... c) {//If this is not preceded by a char C1, it will cause a compilation error System.out.println ("Second");}


   Static-related initialization issues:

1. For a class, the initialization of a static member variable or static block is performed before the main method, if the main method is inside the class.

2. Static member variables, or initialization order between static blocks, are determined by the order in which they appear between them.

3, the static block will only be executed once, that is, when the class is loaded, so for a class, if you call its construction method through new to build an object, if it is the first time, then the static block will be executed before, and then new, will not be executed again.


Package Com.alex.example;class Cup {cup (int maker) {System.out.println ("Cup (" + maker + ")");} void f (int maker) {System.out.println ("f (" + maker + ")");}} Class Cups {static cup Cup1;static cup cup2 = new Cup (2), Static {System.out.println ("static block in Cups is running"), cup 1 = new Cup (1);//cup2 = new Cup (2);} Cups () {System.out.println ("cups ()");}} public class Explicitstatic {public static void main (string[] args) {System.out.println ("Inside main"); CUPS.CUP1.F (99);} static cups cups1 = new cups ();//static cups cups2 = new cups ();}



Second, issues with garbage collection:

Java garbage collection can be simply summed up as: adaptive, generational, stop-copy, tag-sweep garbage collection.


Stop-Copy

is to stop all threads, stop the world, and then copy the data that is not recycled into a piece of unused memory, and then clean up the remaining memory content. More time-consuming.


Mark-Sweep

Tag-sweep also needs to stop the thread, then mark the data to be recycled, and then recycle it so that the general speed will be faster than the above, but it will produce memory fragmentation.


Self-adapting

Adaptive means that the Java virtual machine, will be adaptive, in the garbage more time, the use of Stop replication, and in less garbage, enter the stable period, the use of tag cleanup.


Sub-generational

Generational means, the memory of the data block calibration, if a data block, every time a garbage collection, then give it algebra plus 1, boil over several times of the data, it is possible to carry out the old age of memory, and for some relatively large data, it may be produced directly into the old age, and not in the new generation, Therefore, it is best not to create frequent, large-scale data with short lifecycles, which can cause the JVM to perform a full garbage collection frequently.






"Thinking in Java" Reading notes (i)

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.