Examples of objects to explain

instantiate an object

In Java, new keyword is used to instantiate an object. The new operator creates an object in memory, and returns a reference to the newly created object. As long as our program keeps a reference to the object, then the object will remain in memory.

The following statement declares an Employee reference, and the references using the new keyword assigned to a new Employee object:

 

Employee e;

e = new Employee();

Employee object reference pointing to the memory of the e. New operator allocates memory for the object, then the object member variables assigned to all initial value, so that these will not include member variables garbage data.

Table lists the initial values ​​of the member variables of various data types.

Table 4-1 initial value of an object member variable

Member variable data types

The initial value

byte

0

short

0

int

0

long

0

float

0.0

double

0.0

char

Null character

boolean

false

Any type of reference

null

Examples of statements of two objects can be combined into one statement to achieve. E.g:

 

Employee e = new Employee();

Here is the reason why we want to separate the two statements, in order to emphasize is actually created when instantiated in memory of two entities: a and an object reference. In the first statement in the two statements, e is declared as a reference to the Employee object, meaning that can refer to any Employee object e. And the second statement, e is assigned a new Employee object.

2 understand references

It is generally a reference to a 32-bit integer value that references the memory address of the object contains. Here to say "usually" because the Java language specification does not strictly define the size of a reference. In the future, reference may be 64 or greater. Similarly, if used in a smaller electronic device operating systems, reference may be smaller than 32.

The reference is essentially an integer. So why do you want to declare a reference to a specific data type it? This is because the Java data type is strictly enforced, a reference must be some special kind of data type.

For example, the following statement, we assigned two Employee references and a String reference in memory:

 

Employee e1, e2;

String s;

Three reference the same memory consumption, and in fact are integer data types. However, the reference e1 and e2 can only refer to an Employee object. S String object reference can only reference. If we try to use the following statement to break the rules, it is not compiled by:

 

 

s = new Employee (); // can not be compiled

e1 = "John Doe"; // not compile

Some people might think that using a cast operator can solve the problem:

 

e1 = new Employee (); // effective

s = e1; // not compile

s = (Employee) e1; // not compile or

However, the compiler knows the String object and Employee objects are not compatible, so the above statement is a compilation error will occur.

And reference e1 and e2 are the same data type can be assigned to each other. E.g:

 

e1 = new Employee();

e2 = e1; // effective

References e1 is assigned a new Employee object is assigned a reference e2 e1. This is effective because Employee e1 and e2 are cited, the same data type. The new Employee object there are two references to it, shown in Figure 4.1. Note that only an Employee object in memory, because we can only use once the new keyword. Assigned to the e1 e2 does not create a new object.


Figure 4.1 reference each assignment

We can also use a statement to declare a reference e, and initializes the Employee object:

 

Employee e = new Employee();

This statement creates two separate elements in memory: Employee object and reference e. E reference is not an object. The object itself is not a variable name, the only way we want to access and use the object is to use a reference to the object.

3 method must be associated with an object

The most common mistake a beginner is to try not to use a reference to a method call. In the following code fragment, the second statement will not compile:

 

Employee e1 = new Employee(), e2 = new Employee();     //有效

mailCheck (); // error. Mail a check to whom?

In the above line of code, we instantiate two Employee objects, then tried not to use references to call mailCheck (). First, Java is not enough to do so. In Java, a method must be used to be called a reference, unless the method is declared as static (static). In this case, we can use the method in front of the class name with a dot operator called directly.

Secondly, because there are two Employee objects, so there are two mailCheck () method. If you do not specify the appropriate references and the dot operator, the compiler does not know which way we in the end of an object to be called.

Each has its own name with the Employee object, the same address, salary and other properties, each Employee object has its own mailCheck () and computePay () method. To call mailCheck (), we must use the dot operator to specify mailCheck () method which object to invoke. For example, mailCheck Employee object the following statement on behalf of the calling e1 () method:

 

e1.mailCheck();

The following statement calls e2 representative Employee of mailCheck () method:

 

e2.mailCheck();

Guess you like

Origin blog.csdn.net/qq_45097560/article/details/90757086