Lecture-4 Objects and Memory-DESKTOP-APD3U0U
Lecture-4 Objects and Memory-DESKTOP-APD3U0U
5-2
Variables in Memory
5-3
References
• Every variable is implemented as a location in computer memory
• When the variable is a primitive type, the value of the variable is
stored in the memory location assigned to the variable
– Each primitive type always require the same amount of memory to
store its values
int age = 23
23 4-Bytes
age 23
5-4
References
• When the variable is a class type, only the memory address (or
reference) where its object is located is stored in the memory
location assigned to the variable
– The object named by the variable is stored in some other location in
memory
– Like primitives, the value of a class variable is a fixed size
– Unlike primitives, the value of a class variable is a memory address or
reference
– The object, whose address is stored in the variable, can be of any size
5-5
Class Type Variables Store a Reference (Part 1 of 2)
5-6
Class Type Variables Store a Reference (Part 2 of 2)
5-7
Assignment Operator with Class Type Variables
• Two reference variables can contain the same reference, and
therefore name the same object
– The assignment operator sets the reference (memory address) of one
class type variable equal to that of another
– Any change to the object named by one of these variables will
produce a change to the object named by the other variable, since
they are the same object
variable2 = variable1;
5-8
Assignment Operator with Class Type Variables (Part
1 of 3)
5-9
Assignment Operator with Class Type Variables (Part
2 of 3)
5-10
Assignment Operator with Class Type Variables (Part
3 of 3)
5-11
The Constant null
• null is a special constant that may be assigned to a variable of any class type
YourClass yourObject = null;
• It is used to indicate that the variable has no "real value"
– It is often used in constructors to initialize class type instance variables when there is no obvious
object to use
• null is not an object: It is, rather, a kind of "placeholder" for a reference that does
not name any memory location
– Because it is like a memory address, use == or != (instead of equals) to test if a class variable
contains null
if (yourObject == null)
System.out.println("No real object here.");
5-12
Pitfall: Null Pointer Exception
• Even though a class variable can be initialized to null, this does not mean that
null is an object
– null is only a placeholder for an object
• Any attempt to do this will result in a "Null Pointer Exception" error message
ToyClass2 aVariable = null ;
String representation = aVariable.toString();
5-13
Anonymous Objects
• Sometimes the object created is used as an argument to a method, and never used
again
– In this case, the object need not be assigned to a variable, i.e., given a name
• An object whose reference is not assigned to a variable is called an anonymous
object
5-14
Java Garbage Collection
• In java, garbage means unreferenced objects.
• Garbage Collection is process of reclaiming the runtime unused
memory automatically.
• Advantages
– It makes java memory efficient because garbage collector removes
the unreferenced objects from heap memory.
– It is automatically done by the garbage collector(a part of JVM) so
we don't need to make extra efforts.
5-15
Garbage Collection - Basic Process
5-16
How can an object be unreferenced?
• By nulling a reference:
Employee e=new Employee();
e=null;
• By assigning a reference to another:
Employee e1=new Employee();
Employee e2=new Employee();
e2=e1;//now the first object referred by e1 is available for garbage collection
• By anonymous object:
new Employee();
5-17
Java Object finalize() Method
• Finalize() is the method of Object class
• Called just before an object is garbage collected
protected void finalize(){}
5-18
Rules for method writing
• A method should not have arguments if the working is solely dependent
on class data members only. (example: area function in rectangle class)
• If a method requires something other than the class data members,
then that should be received as argument. (example: withdraw function
in Account class. Amount to be withdrawn should be received in
argument)
• A computation function should return the result of computation.
• Avoid the use of scanners in methods.
• State modification methods must ensure valid state changes.
• Apply validation checks in state modification methods.
5-19