CH 2
CH 2
OOPJ
Control flow statements and loops, Loops – for, while, do-while;
Console input and output, formatting output;
Constructors and methods,
Overloading of methods and constructors,
recursion,
Parameter passing,
static fields and methods,
access control,
this reference;
Garbage collection.
Control flow statements and loops, Loops – for, while, do-while;
Console input and output, Formatted output
Constructors and methods, scope of variable, overloading
Recursion,
Parameter passing,
Pass by reference
class GFG {
int Number;
GFG() { Number = 0; }
update(ob);
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Garbage collection
Garbage collection in Java is the process by which Java programs perform automatic memory management.
Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java
programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the
program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects
and deletes them to free up memory.
Finalization
• Just before destroying an object, Garbage Collector calls finalize() method on the object to perform cleanup
activities. Once finalize() method completes, Garbage Collector destroys that object.
• finalize() method is present in Object class with the following prototype.
• protected void finalize() throws Throwable { }
Advantages of Garbage Collection in Java
The advantages of Garbage Collection in Java are:
•It makes java memory-efficient because the 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 extra effort