Java Study Notes (2) Introduction to classes and objects

Source: Internet
Author: User

Relationships between classes (objects)

1. Join relationship (the parameter of a method of a class is the object of another class)

2. inheritance relationship (XX is a type of XX)

3. Aggregation (overall and partial) (XX is part of XX)

4. Implementation relationships (similar to pure virtual functions of C ++, which are implemented using subclasses)

Objects are the core of Java programs. in Java programs, "Everything is an object"

The object can be viewed as the encapsulation body of the time static attribute (member variable) and dynamic attribute (method ).

Member variables can be any data type in Java (including basic and reference types)

You can initialize a member variable when defining it. If it is not initialized, Java uses the default value to initialize it (the default value of all value types is 0, and the initial value of boolean type is false, the initial value of the reference type is null)

Note: local variables are not initialized by default in Java.

The scope of member variables is the entire class.

In Java, variable types other than basic types are called reference types !! Different from C ++

Objects in Java are operated by referencing them, for example, String S. This declares a String-type referenced variable and allocates memory, but does not point it to an object.
S = new String ("HelloWorld"); use the new statement to create a String-type object and point it to S. Then, you can perform operations on it through S.

How to classify and object in memory

1. The class is a static concept located in the code area.

2. the object is new and located in the heap memory. Each member variable of the class has different values (except static variables) in different objects, and the method has only one copy, memory usage during execution

References in Java are different from references in C ++. References in Java are the same as pointers, while references in C ++ are just aliases.

Object creation and use

1. You must use the new keyword to create an object.

2. Use an object (reference). A member variable or a member variable that references an object

3. Use object (method). Method (Parameter List) to call object Methods

4. Different objects of the same class have different member variable storage spaces

5. Different objects of the same class share the method of this class.

Example:

class C {        int i;        int j;        ...main..{             C c1 = new C();             C c2 = new C();        }}

The code is stored in code segment. When the main method is executed, the C-type variable is first defined before c1 (the value in c1 is not the actual physical address ), A space will be allocated in the stack, and a new object is located in the heap memory. c1 in the stack space points to the new object in the heap space. (it feels like creating a pointer first and then creating the Data Type instance to which the Pointer Points, and then pointing the pointer to that instance)

Create a new object using the new + Constructor

The constructor is a function defined in the java class to initialize objects.

The constructor has the same name as the class and no return value. If no constructor is defined, the system generates an empty constructor by default. If you define a constructor, the system will not produce the default constructor. Note: The default constructor initializes all member variables (all types that can be converted to integers are initialized to 0, and boolean is initialized to false). If a return value is added before the constructor, then this method becomes a common method, through the object name. call a member function name

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.