Object-oriented and memory parsing in 03_2_java

Source: Internet
Author: User

Object-oriented and memory parsing in 03_2_java1. member Variables

the member variable can be any of the data types in the Java language ( including basic data types and reference data types )

a member variable can be initialized when it is defined, and if it is not initialized, Java uses the default initial value.

such as the following list:

Member Variable type

Take value

Byte

0

Short

0

Int

0

Long

0L

Char

' \u0000 '

Float

0.0F

Double

0.0D

Boolean

False

All reference types

Null

Member variables are scoped to the entire class body.

2. References

variable types other than primitive types in the Java language are referred to as reference types.

An object in Java is manipulated by reference.

3. how to classify and object in memory central

Class is a static concept, the code area

The object is new , in heap memory, and each member variable in the class has a different value in different objects (except for static variables) and only one copy of the method, which takes up memory when executed.

4. creation and use of objects

You must create an object using the new keyword.

use Objects (references). a member variable or a member variable that refers to an object.

use Objects (references). method (parameter list) to invoke the object's method.

Each object of the same class has a different storage space for member variables.

Each object of the same class shares its methods.

5. constructor Method (constructor)

Create a new object by using the new + construct method.

constructors are defined in the a function in the Java class to initialize an object.

The constructor has the same name as the class and has no return value.

For example:

public class Person {

int id;

int age;

person (int n, int i) {

id = n;

age = i;

}

}

When no constructor is specified, the compiler automatically adds a shape to the class as

class name () constructor for {}.

6. Customary naming conventions

Capitalize the first letter of the class name

The first letter of the variable name and method name is lowercase

Use hump marking

7. Invoke procedure Demo

Test test = new test ();

int date = 9;

BirthDate D1 = new BirthDate (7,7,1970);

BirthDate D2 = new BirthDate (1,1,2000);

Test.change1 (date);

Test.change2 (D1);

Test.change3 (D2);

......

public void Change1 (int i) {

i = 1234;

}

public void Change2 (BirthDate b) {

b = new BirthDate (22, 2, 2004);

}

public void Change3 (BirthDate b) {

B.setday (22);

}

Figure 1

Figure 2 date passes 9 to parameter i ,i = 9

Figure 3 I is set to 1234

Figure 4 method change1 After the call is complete,i value disappears

Figure 5 calling change2 to Pass the value of D1 to b

Figure 6 B and D1 point to the same object

Figure 7 B and assign a new value to another object

Figure 8 method change2 After the call is complete, b disappears, and the object disappears (the object inside the heap may not disappear immediately, but at the time of garbage collection, Disappear. )

Figure 9 calling change3 b is passed as The value of D2, and D2 points to the same object

Figure Day was changed to ten .

Figure One change3 call complete b disappears

Object-oriented and memory parsing in 03_2_java

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.