Java Learning Note 04

Source: Internet
Author: User
Tags modifiers

I. Concepts of classes and objects

    • Class

>> a class of entities with the same properties and behavior

    • Object

>> entities that are physically present. Objects are typically divided into two parts, the static and dynamic parts. The static part refers to the part that is not moving, which is called the attribute, and any object will have its own property; The dynamic part refers to the behavior of the object.

    • A class is essentially a vector that encapsulates an object's properties and behavior, and an object is an instance of a class instantiation

Two, object-oriented three major characteristics

    • Packaging

>> wraps the properties and behaviors of an object, hiding its implementation details, called encapsulation

>> The idea of encapsulation ensures the integrity of the data structure within the class, and the user who applies the class cannot easily manipulate the data structure directly, only the class allows the public to do so. Avoid the influence of external operation on internal data, improve the maintainability of the program

    • Inherited

>> an object directly uses the properties and methods of another object, and can augment its own properties and methods on the basis of another object, becoming an inheritance. The inheritors are called subclasses, and the inheritors are called the parent class.

An instance of the >> subclass is an instance of the parent class, but it cannot be said that an instance of the parent class is an instance of a subclass

>> inheritance relationships can be represented by a tree relationship, and there is a hierarchical relationship between the parent class and the child class. A class is in the inheritance system, which can be a parent class for other classes, provide properties and behaviors for other classes, or be subclasses of other classes, inheriting the properties and methods of the parent class

    • Polymorphic

>> The feature that applies a parent object to a subclass is polymorphic

Third, class

    • Member variables

The properties of an object in >>java are also known as member variables

    • Member properties

The behavior of an object in >>java becomes a member method

    • Permission modifiers

The permissions modifiers in >>java mainly include private, public, and protected

>>private: This member variable or method can only be used in this class, is not visible in subclasses, and is not visible to classes of other packages

>>public: In addition to using this data in this class, you can use it in subclasses and other packages ' classes

>>protected: Only subclasses or other classes of the class within this package can access member variables and member methods in this class, and other package classes and subclasses cannot access

    • Local variables

The variables within the >> member method are referred to as local variables. A local variable is created when the method is executed and destroyed at the end of the method's line. Local variables must be assigned or initialized when they are used, or compile errors will occur

>> scope of a local variable starts at the declaration of the variable and ends at the end of the variable

>> You can declare two local variables of the same name and type at the same time in a non-nested scope

    • this keyword

>> Specifies the use of the This keyword in the Java language to represent references to objects of this class, which are implicitly used to refer to the member variables and methods of an object

>>this can also be used as the return value of a method in addition to calling member variables or member methods

    • How to construct a class

The >> constructor method is a method that is the same as the class name, and the creation of the object is done through the construction method. Whenever an object is instantiated, the class will automatically call the constructor method

>> construction method features are as follows

>>> constructor method has no return value

>>> the name of the constructor method is the same as this class

1  Public Book () {  //Public: Constructor method modifier Book  : Constructor Method name///  no void!! 2     ... // Construct method Body 3 }
    • Static variables, solid state, and methods

>> variables declared as static, solid, and methods are referred to as statically members

>> static members are class-owned, distinguished from individual objects, and can use class names and "." In this class or other class. Operator calls a static member

>> static data and static methods are often used to provide shared data or methods, such as mathematical formulas, etc.

>> static members also need to follow the constraints of public, private, and protected modifiers

    • The main method of the class

The >> Main method is the entry point of the class, which defines where the program starts; The main method provides control of the flow of the program, and the Java compiler executes the program through the main method.

The characteristics of the >> Main method are as follows

The >>> Main method is static, so if you want to invoke other methods directly in the main method, the method must also be static

>>> Main method has no return value

>>> the formal parameter of the main method is an array. Where Args[0]~args[n] represents the first parameter of the program to the nth parameter, you can use Args.length to get the number of arguments

The syntax of the >> Main method is as follows

 Public Static void Main (string[] args) {       // method Body }

Iv. objects

    • Creation of objects

>> creating objects in the Java language with the new operator, you can use the new operator in the Java language to call a constructor method to create an object

When a >> object is created, it is a reference to an object. This reference allocates storage space for the object in memory, and can also initialize member variables in the constructor method

>> each object is independent of each other, occupies a separate memory address in memory, and each object has its own life cycle, when the end of the life cycle, the object becomes garbage, by the Java Virtual machine's own garbage collection mechanism, can no longer use

    • Accessing the properties and behavior of an object

>> using Object. class member to get the properties and behavior of an object

    • Reference and comparison of objects

References to >> objects: Class name object reference name

>> references to Objects associated with syntax: Class name object reference name = Create object operator (new) Construction method

>> Comparison of objects: "= =" and Equals ()

    • Destruction of objects

>> Each object has a life cycle, and when the object's life cycle ends, the memory address assigned to the object is recycled

The two types of objects recycled by the >> garbage collection mechanism

>>> object reference exceeds its scope, this object will be treated as garbage

>>> assigning an object to a value of NULL

>> garbage collection mechanism can only reclaim objects created by the new operator

>> If there are objects that are not created by the new operator, you can use the Finalize () method provided by Java, which is a method of the object class, declared as protected, which the user can define in their own class. If the user defines the Finalize () method in the class, the method is called first when garbage collection occurs, and the memory consumed by the object is actually reclaimed when the next garbage collection action takes place

>> It should be emphasized that the garbage collection or Finalize () method is not guaranteed to occur, such as when the Java Virtual machine memory is depleted, garbage collection is not performed

>> the Finalize () method cannot be executed because garbage collection is not controlled and the execution time is uncertain. This Java provides the System.GC () method to force the garbage collector to tell the garbage collector to clean up

Java Learning Note 04

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.