• Tidak ada hasil yang ditemukan

Classes and Objects CPCS 202

N/A
N/A
Protected

Academic year: 2025

Membagikan "Classes and Objects CPCS 202"

Copied!
13
0
0

Teks penuh

(1)

Classes and Objects

CPCS 202

(2)

WEEK 9 LECTURE 1

LECTURE 1

(3)

Instantiating an Object

In java the new keyword is used to instantiate an object in memory and

returns a reference to the newly created object.

object.

Employee e;

e = new Employee();

The reference e is pointing to the

Employee object in memory.

(4)

Instantiating an Object

The new operator allocates memory for the object and then “zeroes” the

memory so that none of the object’s

memory so that none of the object’s

fields will contain garbage. Instead, all

fields will have an initial value of zero.

(5)

Initial Value of an Object’s Fields

FIELD DATA TYPE INITIAL VALUE byte 0

short 0

int 0

long 0

float 0.0 double 0.0

char the null character

boolean false

reference of any type null

(6)

Understanding References

• A reference is (typically) a 32-bit integer value that contains the memory address of the object it refers to.

• In the following statements, two

• In the following statements, two

Employee references and one String reference are allocated in memory.

Employee e1, e2;

String s;

(7)

• Each of these three references consumes the same amount of memory and is essentially

an integer data type. However, the references an integer data type. However, the references e1 and e2 can refer only to Employee

objects. The reference s can refer only to a String object.

(8)

To illustrate this point, the following

statements attempt to break this rule and are not valid:

s = new Employee(); //Does not compile e1 = “Rich”; //Does not compile e1 = “Rich”; //Does not compile e1 = new Employee(); // Valid

s = e1; // Does not compile

However, the compiler knows that String objects and Employee objects are not compatible and will

generate compiler errors in the statements above.

(9)

The references e1 and e2 are the same data type and can be assigned to each other.

For example:

e1 = new Employee()

e2 = e1; //Valid e2 = e1; //Valid The e1 reference is assigned to a new

Employee object, and the e2 reference is

assigned to e1. This is valid because e1 and e2 are both Employee references and therefore

(10)

Garbage Collection

In Java, there is no keyword or operator that you can use to remove an object from memory. Java was designed to

avoid the problems of memory leaks that arise in other languages. A JVM has a

arise in other languages. A JVM has a

low-level thread known as the garbage

collector that is constantly running in the

background, looking for objects in your

Java program that are no longer being

(11)

Garbage Collector Demo program

public class GCDemo {

public static void main(String [] args) {

Employee e1, e2, e3;

e1 = new Employee(); //Employee #1 e2 = new Employee(); //Employee #2 e2 = new Employee(); //Employee #2 e3 = new Employee(); //Employee #3 e2 = e1;

e3 = null;

e1 = null;

} }

(12)

The GCDemo program creates three references and assigns each to a new

Employee object. The new keyword is used three times, so there are three

objects in the program. The result of assigning objects in the program. The result of assigning e2 to e1 is that employee #2 no longer has a reference to it and can be garbage collected at any point after the statement e2 = e1. Note

that now employee #1 has two references pointing to it.

(13)

Assigning e3 to null causes employee #3 to be immediately eligible for garbage collection

because the object can no longer be reached.

Assigning e1 to null does not cause employee

#1 to be garbage collected because e2 still

#1 to be garbage collected because e2 still

refers to the object. The reference e2 goes out of scope at the end of main(), so the

employee #1 object can be garbage collected immediately after main() is done executing.

Referensi

Dokumen terkait

1. Article I of this Agreement does not apply to the first entry of holders of valid diplomatic or service passports of either Party who are assigned to a

While being a forerunner of an Avant-garde style, Picasso does not break through patriarchal conventions in his representations of the female body: when featuring the female

There is a lot going on in this passage, and I do not propose to account for everything in terms of existential feeling. Even so, the passage does serve to illustrate the aspect

Here, a full mediation means that job satisfaction has a strategic role in reducing the work stress and work- family conflict so that it does not have a significant impact on employee

This study has made an attempt to investigate the relationship between role clarity of the employees, socialization of the employees and employee satisfaction with internal branding as

Other studies, which begins PD in break-in period, dialysate leak increased but our study does not show this, which is significantly different from previous studies.29 Abdominal wall

CONCLUSION From the results of research and discussion, the author concludes that the first hypothesis states that the variable of organizational commitment does not influence

1 February 2024 | ISSN: 2963-8119 print, ISSN: 2963-7821 online, Page 65-73 DISCUSSION Based on the research results, organizational culture does not affect employee performance