0% found this document useful (0 votes)
10 views94 pages

CH 2

Uploaded by

jsjshdhdhdhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views94 pages

CH 2

Uploaded by

jsjshdhdhdhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

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; }

static void update(GFG ob) { ob.Number++; }

public static void main(String[] args)


{
GFG ob = new GFG();

System.out.println("Number value " + (ob.Number));

update(ob);

System.out.println("Updated Number value "


+ (ob.Number));
} Number value 0
} Updated Number value 1
Static fields and methods,
Access control
This reference;
this is a reference variable that refers to the current object.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}

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.

Ways for requesting JVM to run Garbage Collector


•Once we make an object eligible for garbage collection, it may not destroy immediately by the garbage collector.
Whenever JVM runs the Garbage Collector program, then only the object will be destroyed. But when JVM runs
Garbage Collector, we can not expect.
•We can also request JVM to run Garbage Collector. There are two ways to do it :
• Using System.gc() method: System class contain static method gc() for requesting JVM to run Garbage
Collector.

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

You might also like