Java with notes 2

Source: Internet
Author: User

1, Arrays

Java8 added Parallelsort and other parallel methods, indicating the ability to take advantage of CPU parallelism

2. Object-oriented

If a class in the inheritance tree is to be initialized, all the parent classes of that class will be initialized at the same time

Subclasses do not get the constructor of the parent class, but the subclass constructor can call the parent class constructor with super. But the super call must also appear in the first row of the constructor, so super and this do not appear at the same time.

In addition, the subclass constructor always invokes the parent class constructor, regardless of whether the super call is used to execute the parent class constructor. Because the system implicitly calls the constructor of the parent class without arguments before executing the subclass constructor, the parent class first calls the constructor of the parent class, so the first thing to do is always the constructor of the Java.lang.Object class.

Polymorphic
There are two types of Java reference variables: one is a compile-time type and one is a run-time type. A compile-time type is determined by the type used when declaring the variable, and the run-time type is determined by the object that is actually assigned to the variable. A so-called polymorphism can occur if the compile-time and run-time types are inconsistent.
When assigning an instance object of a subclass directly to the parent class reference variable, the compiled type of the reference variable is the parent class, and the run type is a subclass, so when the runtime invokes the method of the reference variable, it always shows the method of the subclass, so: variables of the same type, when invoking the same method, present many different behavior characteristics, This is polymorphism.
Unlike a method, a variable is not polymorphic and is still a variable value of the parent class when called.
A reference variable can only invoke a method of its compiled type at compile time, but the runtime executes the method of its run-time type, so that when polymorphic, a subclass method that the parent class does not have can be called by reflection.

Recursive

public static int fn (Integer n) {        if(n.equals (0)) {            return 1;        }         If(n.equals (1)) {            return 4;        }         return 2*fn (n-1) +fn (n-2);    }

1, if a wrapper class and a basic type comparison size, or two basic types to compare size, directly with = = good;
If the two wrapper classes are compared in size, use Equals (), the return value is True,false, or use Xxx.compare (), and the return value is 1,0,-1. (The Equals () method here is already overridden by the wrapper class, not the Equals () of the object)
2. The difference between = = and Equals method
When using = = to determine whether two variables want to wait, compare the value in the stack memory, if the two variables are basic type variables, and are numeric types, then the stack memory is stored in the basic type variable value, so as long as the value of the two basic type variable to wait, it will return true. But for two reference-type variables, because the stack memory is a reference to a variable, the = = will only return TRUE if the two reference-type variables point to the same object. = = is not available for two objects that do not have a parent-child relationship on the comparison type.
The Equals () method is an instance method of the object class, but this method does not differ from = =. However, the wrapper class of the string class and the base type overrides the Equals () method, no longer compares the reference address of the reference object, but compares the referenced value.

The system automatically adds the public static final modifier to the member variables in the interface.
So the definition of int max = 50 in the interface, and the public static final int max = 50, is the same meaning.

3.Lambda Expression Example

Runnable r = () {            for (int i = 0; i <; i++ ) {                System.out.println (i); 
    }        };                 New Thread (R, "Thread 1"). Start ();         New Thread (R, "Thread 2"). Start ();

A lambda expression can only implement a functional interface (an interface with only one abstract method)

There's a lot of similarity to anonymous internal classes.

4. Forced garbage Collection

System.GC ()

Runtime.getruntime (). GC ();

Rewrite the Finalize () method to see if garbage cleanup was performed

Java with notes 2

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.