Initial knowledge of Java Oo

Source: Internet
Author: User

Stacks: Stores are local variables (variables defined in functions, arguments on functions, variables in statements);

The data is freed as long as the area where the data operation is completed ends.

Heap: Used to store arrays and objects, that is, entities. What is an entity? is used to encapsulate multiple data.

1: Each entity has a memory header address value.

2: Variables in heap memory have default initialization values. Because the data types are different, the values are not the same.

3: Garbage collection mechanism.

Object-oriented:

1: Simplifying complex things.

2: Object-oriented converts the performer in the previous process into a conductor.

3: Object-oriented This idea is in line with the current thinking habits of people.

How do processes and objects manifest in our programs? The process is actually a function, and the object is to encapsulate some content such as functions.

The definition in a class is actually called a member. There are two types of members:

1: Member variables: In fact, the corresponding is the property of the thing.

2: member function: In fact, the corresponding is the behavior of things.

So, in fact, defining a class is defining member variables and member functions. But before the definition, it is necessary to analyze the property and behavior of things before they can be embodied in code.

The difference between a member variable and a local variable:

1: Member variables are defined directly in the class.

A local variable is defined in a method, on a parameter, in a statement.

2: Member variables are valid in this class.

A local variable is valid only within the curly braces it belongs to, the curly brace ends, and the local variable loses scope.

3: Member variables exist in heap memory and disappear as objects are created.

A local variable exists in the stack memory and is released as the owning region runs and ends.

Constructor: Used to initialize an object, it is to initialize the corresponding object, it is targeted, one of the functions.

Characteristics:

1: The name of the function is the same as the name of the class in which it resides.

2: You do not need to define a return value type.

3: The function does not have a specific return value.

Remember: When all objects are created, they need to be initialized before they can be used.

Note: When a class is defined, if a constructor is not defined, the constructor for an empty argument is automatically generated in the class, and initialization is done to facilitate the class to create the object. If a constructor is customized in a class, then the default constructor is gone.

In a class, you can have multiple constructors, because their function names are the same, so they can only be distinguished by a parameter list. So, if more than one constructor appears in a class. Their existence is manifested by overloading.

What is the difference between a constructor and a general function?

1: Two function definition formats are different.

2: Constructors are called when an object is created, used for initialization, and initialization actions are performed only once.

The general function is that after the object is created, it needs to be called to execute and can be called multiple times.

When do you use constructors?

When you analyze things, when you find specific things that appear, you have features that define them within the constructor.

What is the difference between constructing a code block and a constructor function?

Constructs a block of code: Initializes all objects, that is, all objects call a block of code that will be called as soon as the object is established.

Constructor: is to initialize the corresponding object, it is targeted.

"Person p = new person ();"

What does creating an object do in memory?

1: Load the Person.class file of the specified location on the hard disk into memory first.

2: When the Main method is executed, the space of the Main method (stack-to-stack) is opened in the stack memory, and a variable p is assigned to the stack area of the main method.

3: Creates an entity space in the heap memory and assigns a memory header address value. New

4: Spatial allocation of attributes in the entity space, and default initialization.

5: Display initialization of the properties in the space.

6: The construction code block of the entity is initialized.

7: Call the constructor of the entity to initialize the constructor function. ()

8: The first address is assigned to P, and the P variable refers to the entity. (pointing to the object)

Initial knowledge of Java Oo

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.