[java learning] class Class

1. Concept

1) class object

Java uses new to create a new object, which will be stored in the heap.

String a1 = new String("abc");
String a2 = new String("abc");
String a3 = new String("abcd");

As above, a total of 2 String objects are created.
"abc" and "abcd" create one each when the class is loaded, and create three implementations for a1, a2, and a3 when executing the code.

2) Difference between class and array

A class is a collection of methods and variables.
An array is an unordered collection of data of the same type.

2. Inner class

1) Static inner class

An inner class decorated with static.
① Static inner classes can have static members, while non-static inner classes cannot have static members.
②The non-static members of the static inner class can access the static variables of the outer class, but not the non-static variables of the outer class.
③ Non-static members of non-static inner classes can access non-static variables of outer classes.

2) Inner class

① Why does the inner class hold a reference to the outer class?

Although the inner class and the outer class are written in the same file, after the compilation is completed, their own class files are still generated, and the inner class accesses the members of the outer class through this. Because the generation of the inner class depends on the outer class, the reference held is .this.
Principle:
i> The compiler automatically adds a member variable to the inner class. The type of this member variable is the same as the type of the outer class. This member variable is a reference to the object of the outer class (this)
; The constructor adds a parameter, the type of the parameter is the type of the outer class, and this parameter is used inside the constructor to assign values ​​to the member variables added in the inner class;
iii> When calling the constructor of the inner class to initialize the object of the inner class, it will pass the value by default. into a reference to the outer class.

3) Anonymous inner class

Use the inner class generated by new.

①What anonymous inner class in Java can only access final-modified external variables?

Anonymous inner class usage:

public class TryUsingAnonymousClass {
    public void useMyInterface() {
        final Integer number = 123;
        System.out.println(number);

        MyInterface myInterface = new MyInterface() {
            @Override
            public void doSomething() {
                System.out.println(number);
            }
        };
        myInterface.doSomething();

        System.out.println(number);
    }
}

Compiled result:

class TryUsingAnonymousClass$1
        implements MyInterface {
    private final TryUsingAnonymousClass this$0;
    private final Integer paramInteger;

    TryUsingAnonymousClass$1(TryUsingAnonymousClass this$0, Integer paramInteger) {
        this.this$0 = this$0;
        this.paramInteger = paramInteger;
    }

    public void doSomething() {
        System.out.println(this.paramInteger);
    }
}

Because the anonymous inner class will eventually be compiled into a separate class, and the variables used by the class will be passed to the class in the form of constructor parameters, for example: Integer paramInteger, if the variable is not defined as final, paramInteger is inside the anonymous The class can be modified, thus causing inconsistency with the external paramInteger. In order to avoid this inconsistency, because Java stipulates that anonymous inner classes can only access final-modified external variables.

3. Abstract class

1) Concept

2) Difference between abstract class and interface

Both abstract classes and interfaces are used for abstraction, an interface is an abstraction of an action, and an abstract class is an abstraction of the root.
An abstract class represents what this object is. It is mostly used in scenarios where there are methods that cannot be specifically described in similar things. It is used when there is a logical hierarchy between parent classes and subclasses.
The interface represents what the object can do. More than between different classes, define the communication rules between different classes.
Abstract classes and interfaces cannot be instantiated. (Because there are abstract methods that are not implemented)
Interfaces are preferred, and abstract classes are used as little as possible.
The difference is as follows:

①Inheritance

Interfaces can have multiple inheritance, but abstract classes cannot.

② Realize

Implementing an interface must implement all the methods defined in the interface, and implementing an abstract class can selectively override the methods that need to be used.
When a subclass is an abstract class, it inherits the abstract class and can implement abstract methods or not.

③ Definition

An interface defines a method and cannot be implemented (it cannot have a function body), while an abstract class can implement some methods.
Abstract classes can have constructors, interfaces have no constructors.

④Data type

The interface is public, and cannot have private methods or variables in it. It is used by others, while abstract classes can have private methods or private variables. The data members of the basic data types in the interface are static and final by default (this means that when the interface is defined, these variables must be assigned values ​​and cannot be modified later), but abstract classes are not.
An interface can only have abstract methods and member variables of final type, so no constructors can be defined.

4, class loading

1) Dynamic connection and static resolution

①Dynamic connection

Direct references to certain target methods are not determined until the class is running.

②Static analysis

Converted to a direct reference during class loading or first use.
The premise is that the method has a determinable calling version before the program is actually executed, and the calling version of this method is immutable during runtime.
The methods invoked by the invokestatic and invokespecial directives can have a unique invoked version determined during the parsing phase.
There are mainly: ① static methods; ② private methods; ③ instance constructors; ④ four types of parent methods. These methods can be called non-virtual methods (including final methods), in contrast, other methods are called virtual methods (except final methods).
The final method should be specially explained here. Although the invokevirtual instruction is used to call the final method, since it cannot be overwritten and there is no other version, there is no need for the other party to perform polymorphic selection of the receiver. The Java Language Specification clearly states that a final method is a non-virtual method.

5. Object class

1) Concept

The Object class is the root of the hierarchy, and all classes inherit from this class.
The Object class is the only class in the java language that has no parent class.
In the case where the superclass is not explicitly given, Java will automatically use Object as the superclass of the class to be defined.

2) Method

①Object()

default constructor

②clone()

Creates and returns a copy of this object.

③equals(Object obj)

Indicates whether some other object is "equal" to this object. It is the same as == in Object, and subclasses generally need to override this method.

④finalize()

This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object.

⑤getClass()

final method that returns the runtime class of an object.

⑥hashCode()

Returns the hash code value for this object.
This method is used for hash lookup. Overriding the equals method generally requires overriding the hashCode method. This method is used in some Collections with hash function.

⑦notify()

Wakes up a single thread waiting on this object's monitor.

⑧notifyAll()

Wakes up all threads waiting on this object's monitor.

⑨toString()

Returns a string representation of this object. Converted to a string, generally subclasses have rewrites, otherwise print the handle.
The format of this string is fixed: class name @hashcode. This hashcode is a string of numbers (in hexadecimal) called handle/(virtual) address in Java. But handles are not memory addresses .

⑩wait

Make the current thread wait for the lock of the object, the current thread must be the owner of the object, that is, have the lock of the object.
--wait()
until another thread calls this object's notify() method or notifyAll() method.
--wait (long timeout)
until other threads call this object's notify() method or notifyAll() method, or until the specified amount of time has passed.
--wait (long timeout, int nanos)
until another thread calls the notify() method or notifyAll() method of this object, or some other thread interrupts the current thread, or some real amount of time has passed.

6. Math class

1) Ornament

①Return static double ceil(double a);
the min integer >=a ②Return
the static double floor(double a);
max integer <=a, the type is not double
③Round static double rint(double a);
to the nearest integer to a
④Round static long round(double a);
to the nearest long integer to a. Add 0.5 to the original number and round down. example:

Math.round(12.5) = 13;
Math.round(-12.5) = -12

⑤Round static int round(float a);
to the nearest integer and return to the nearest integer to a

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325540747&siteId=291194637