Docsity

Java Garbage Collection and Object References, Study notes of Object Oriented Programming

Toggle sidebar
bg1
Garbage Collection
•As we can recall that in our last Ex. For each employee in the company,
we would instantiate an Employee object.
If we have 50 employees, we need 50 Employee objects in memory, this
would create 50 names, addresses, salaries, so on. And for more
employees more objects will be created
Each employee would be distinguished by a reference, so we would need
50 references as well.
•We have seen that how the new keyword is used to instantiate objects.
•However, we have not discussed how to delete an object after you are
finished with it so the memory that the object is consuming can be
freed.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Java Garbage Collection and Object References and more Study notes Object Oriented Programming in PDF only on Docsity!

Garbage Collection

  • (^) As we can recall that in our last Ex. For each employee in the company, we would instantiate an Employee object. If we have 50 employees, we need 50 Employee objects in memory, this would create 50 names, addresses, salaries, so on. And for more employees more objects will be created Each employee would be distinguished by a reference, so we would need 50 references as well.
  • (^) We have seen that how the new keyword is used to instantiate objects.
  • (^) However, we have not discussed how to delete an object after you are finished with it so the memory that the object is consuming can be freed.

Garbage Collection

  • (^) In other OOP languages such as C++, memory has to be explicitly freed by the programmer.
  • (^) In fact, C++ has a delete keyword that needs to be used to free the memory of an object.
  • (^) If you forget to delete an object in C++ and you lose any references to it, the memory can never be freed and you have created what is referred to as a memory leak , which can wreak havoc on your programs, especially programs that use large amounts of memory or run for long periods.
  • (^) 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 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 used and freeing their memory
  • (^) 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 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.
  • (^) Assigning e3 to null causes employee #3 to be immediately eligible for garbage collection because the object can no longer be reached.
  • (^) In fact, if we decide that the object should be retrieved for some reason, it is too late.
  • (^) There is no way to relocate the object after all references to it have been lost.
  • (^) Assigning e1 to null does not cause employee #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

Methods Are Associated with an Object

  • (^) A common mistake that is often seen that students try to invoke a method without using a reference, because this is how methods are invoked in procedural languages.
  • (^) Consider the following statements, the second of which does not compile: Employee e1 = new Employee(), e2 = new Employee(); //Valid mailCheck(); //Error. Mail check to whom?
  • (^) To demonstrate my point, I instantiated two Employee objects and then attempted to invoke mailCheck() without a reference. First of all, this can never be done in Java.
  • (^) A method can be invoked only by using a reference unless the method is static, in which case a class name is used with the dot operator.
  • (^) Second, because there are two Employee objects, there are two mailCheck() methods.
  • (^) Which one do I want to invoke? I need to specify this using the appropriate reference and the dot operator.

Using the Dot Operator

  • (^) Let’s go through an example that demonstrates writing a

class, creating instances of that class, and using the dot

operator to access the fields and methods of the objects.

  • (^) We have been discussing the Employee class

throughout , so we will begin by writing and compiling

the Employee class.

  • (^) We will then write a second class that uses the Employee

class. Follow along with the subsequent steps, which will

help you to understand the process of using multiple

classes in a program.

public class Employee { public String name; public String address; public int number; public int SSN; public double salary; public void mailCheck() { System.out.println(“Mailing check to “

  • name + “\n” + address); } public double computePay() { return salary/52; } }
  • (^) After both the classes are written, compiled and run observe the be output;
  • (^) In the EmployeeDemo program, two Employee objects are instantiated and referenced using two Employee references: e1 and e2. Each of the fields is initialized in both objects using the dot operator. For example:
  • (^) e1.name = “Robert Smith”;
  • (^) e2.name = “Jane Smith”;
  • (^) There are two Employee objects in memory, which means that there are two fields called name, two fields called salary, two methods called computePay(), and so on. How do you tell them apart?
  • (^) They are distinguished by which reference you use.
  • (^) The Employee reference e1 points to the Employee object whose name is Robert Smith. The Employee reference e2 points to the Employee object whose name is Jane Smith.

Instantiating…

  • (^) Just as each Employee object has its own name, address,
salary, and so on, each Employee object has its own
mailCheck() and computePay() methods.
  • (^) To invoke mailCheck(), it is needful to specify whose
mailCheck() method we want to invoke using the dot operator.
  • (^) For example, the following invokes the mailCheck() method of
the Employee object that e1 refers to:
e1.mailCheck();
  • (^) The statement invokes the mailCheck() method of the
Employee object that e2 refers to:
e2.mailCheck();

Using dot operator

  • (^) If you want to compute the pay of Robert Smith, you invoke computePay() using e1:
  • (^) e1.computePay();
  • (^) If you want to mail a check to Jane Smith, you invoke mailCheck() using e2:
  • (^) e2.mailCheck();
  • (^) The references do more than keep the object from being garbage collected.
  • (^) They represent your only access to the fields and methods of the object—using a reference along with the dot operator.

Exception to the “Field Access

with Dots” Rule

  • (^) You normally access a field through
  • (^) variableName.fieldName
  • (^) but an exception is when a method of a class wants to access fields of that same class
  • (^) In that case, omit the variable name and the dot
  • (^) For example, a move method within the Ship class might do:
  • (^) public void move() {
  • (^) x = x + speed * Math.cos(direction);
  • (^) ...
  • (^) }
  • (^) Here, x, speed, and direction are all fields within the class that the move method belongs to, so move can refer to the fields directly, you still can use the
  • (^) variableName.fieldName approach, and Java invents a variable called this that can be used for that purpose
  • (^) The reason everything worked is that the compiler realizes that compute-
  • (^) Pay() is accessing the salary field, so the compiler adds the reference for us.
  • (^) Which reference does the compiler add: computePay() needs a reference to whatever object that computePay() was invoked on, which in every case is the this reference.
  • (^) The computePay() method actually looks like the following:
  • (^) public double computePay()
  • (^) {
  • (^) return this.salary/52;
  • (^) }
  • (^) Notice that the this reference and the dot operator were prefixed to salary. The compiler adds the this reference for you if you do not explicitly add it yourself. You can add the this reference if you like, or you can simply let the compiler do it for you.

public class EmployeeDemo { public static void main(String [] args) { Employee e1, e2; e1 = new Employee(); e2 = new Employee(); e1.name = “Robert Smith”; e1.address = “123 Main St., Anytown, USA”; e1.number = 101; e1.SSN = 111223333; e1.salary = 10000.00; System.out.println(e1.computePay()); e1.mailCheck(); e2.name = “Jane Smith”; e2.address = “321 Main St., Anytown, USA”; e2.number = 202; e2.SSN = 333221111; e2.salary = 100000.00; System.out.println(e2.name + “ “ + e2.SSN); System.out.println(e2.computePay()); e2.mailCheck(); } } Second class that instantiates and uses Employee objects

234321 Java by Example 19

IDE: Integrated development environment

integrated development environment (IDE) is an application or set of tools that allows a programmer to write, compile, edit, and in some cases test and debug within an integrated, interactive environment. Combines editor, compiler, linker, debugger, other tools Has GUI (graphical user interface) Compiles + links + runs at the click of a button Helps put together a project with several modules (source files)

3- 20

Compiler vs.Interpreter

  • (^) Compiler: checks syntax generates machine-code instructions not needed to run the executable program the executable - (^) Interpreter: checks syntax executes appropriate instructions while interpreting the program statements must remain installed while the program is interpreted the interpreted program is slower Editor Source code Interpreter 