Common Java core notes

Source: Internet
Author: User
Tags comparable

Common Java core Notes * The keyword static is used for static variables and static methods, that is, to design it in two scenarios: one is to allocate a single bucket for a specific domain, do not consider the number of objects to be created, or even create objects. Second, a method is not associated with any objects in its class, that is, it can be called without creating an object.

* The final keyword indicates that the class, method, or variable it modifies cannot be changed.
* The keyword native indicates that this method is not written in Java, but it is implemented by JVM on a platform.
* The keyword projected can only be accessed in the same package or subclass.
* The keyword "this" indicates the reference name of the calling object. One usage is to reference the hidden data fields of the reference class, such as this. raduis = raduis; another method is to let the constructor call another constructor of the same class, such as public Circle () {this (1.0 );}
* The keyword "super" is used in two ways: one is to call the constructor of the parent class, which must appear in the first line of the subclass constructor, and the other is to call the method of the parent class;

* The constructor of the parent class is not inherited by the quilt class. Only the constructor of the quilt class can be called with the keyword "super.
* Constructor can call an overloaded constructor or a constructor of its parent class. If they are not explicitly called, the compiler automatically calls super () the first statement of the constructor.

* In any case, when constructing an instance of a class, the constructor will call the constructor of all the parent classes along the inheritance chain. That is, when constructing a subclass object, the subclass constructor will call the constructor of the parent class before completing its own tasks and keep calling it: this is the constructor.
* If a class is designed to be extended, it is best to provide a non-argument constructor, because the default constructor of the subclass automatically calls the non-argument constructor of the parent class, if the parent class does not have a constructor method, the program cannot be compiled.
* The static method of the parent class can be called directly. If the subclass is redefined, the static method of the parent class will be hidden;
* Method override: This method must be defined in the subclass using the same signature and the same return value type;
* Each subclass instance is an instance of the parent class: where the parent class object is used, the subclass object-polymorphism (polymorphism) can be used)
Polymorphism means that variables of the parent type can reference objects of the Child type.
* Dynamic binding (dynamic binding) means that the call starts from special to general, that is, matching from the underlying subclass to satisfying.

* Implicit casting = upcasting subtype-> parent type/explicit conversion (explicit casting) = downcasting parent type-> child Type
To make the conversion successful, make sure that the object to be converted is an instance of the subclass. If not, a running exception is generated.
ClassCastException
Example: Object myObject = new Circle ();
If (myObject instanceof Circle ){
System. out. println (Circle) myObject). getArea ());
}
Polymorphism is initially defined as the parent type for general program design;
The object member access operation (.) takes precedence over the type conversion operator. Therefore, double brackets are required;

* The abstract class cannot be initialized using the new operator, but its constructor can still be defined to be called in the constructor of the subclass. The abstract class can also be used as a data type, for example: geometricObject [] objects = new GeometricObject [10]; objects [0] = new Circle ();

* The parent class is specific, and the subclass can be abstract, just as the Object is specific;

* An interface is a Class-like structure that only contains constants and abstract methods. It is used to specify the common behavior of multiple objects, that is, the interface specifies the behavior, specific functions are implemented by specific sub-classes.

* All data fields in the interface are public final static, all methods are public abstract, and all java allow to ignore these modifiers.
* Java only allows single inheritance for class extensions, but supports multiple extensions Using Interfaces. It can also inherit multiple interfaces.
* The interface can be used to define the common parent types of irrelevant classes, which is more flexible than the class and is suitable for weak link modeling (weak is-a relationship)

* Comparison operator = is used to compare the values of two basic types, or determine whether two objects have the same reference.

* The default implementation of the equals method in the Object class is:
Public boolean equeals (Object obj ){
Return (this = obj );
}
The = operator checks whether two referenced variables point to the same object.
Equals compares the content of two objects
To test whether two simple types of values are equal, use =;
To compare the values of two referenced variable objects to the same object, use =;
If the values of two referenced variable objects are equal, use the equals () method of the object to compare them;

* Comparable interface of package java. lang
Public interface Comparable {
Public int compareTo (Object o );
}
If the value is less than, equal to, or greater than the given object o, negative integers, 0, and positive integers are returned respectively.
* Since the compareTo method is not defined in the Object class, it is strongly recommended that compareTo be consistent with equals to ensure that Object1.compareTo (Object2) is only valid when Object1.equal (Object2) is true) = 0.

* The Cloneable interface of package java. lang is defined as follows:
Public interface Cloneable {} // an empty interface is called a marker interface, which indicates that a class has certain attributes. Therefore, the class implementing the Cloneable interface is marked as cloned, and its Object can be cloned using the clone () method defined in the Object class.
* The clone method defined in the Object class:
Protected native Object clone () throws CloneNotSuppertedException;
The native keyword indicates that it is implemented by JVM on its own platform. The protected keyword restriction method can only be accessed in the same package or in its subclass. Therefore, when using the clone method, we must overwrite it and change its visibility modifier to public.
* The cloned Object and the original Object are the same: different objects with the same content. In addition, the clone method in the Object class copies each data field of the original Object to the target Object. If the data field is of the basic type, copy its value. If the data field is an object, copy its reference and make it a shortest copy. If you want to implement deep replication, you can call super. clone () and then overwrite the clone method with a custom clone operation.
For example, calendar1 = (Calendar) calendar. clone (); // pay attention to forced type conversion.
Calendar1 and calendar are different objects with the same content;
The clone method in the Object class: 1. If the data field is of the basic type, copy its value; 2. If the data field is an Object, copy its reference; --- shallow copy

* Signature of the toString () method in the Object class:
Public String toString ()
By default, a string consisting of the class name + @ + hex memory address of the object is returned.
System. out. println (object) is equivalent to System. out. println (object. toString (), indicating that the classes, methods, or variables it modifies cannot be changed.

Related Article

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.