0% found this document useful (0 votes)
21 views17 pages

Unit 2 Continue

The document discusses Java concepts like varargs, the this keyword, final keyword, garbage collection, finalize() and gc() methods. Varargs allows methods to accept variable number of arguments. The this keyword refers to the current object. The final keyword restricts variable, method or class modification. Garbage collection automatically reclaims unused memory by destroying unused objects.

Uploaded by

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

Unit 2 Continue

The document discusses Java concepts like varargs, the this keyword, final keyword, garbage collection, finalize() and gc() methods. Varargs allows methods to accept variable number of arguments. The this keyword refers to the current object. The final keyword restricts variable, method or class modification. Garbage collection automatically reclaims unused memory by destroying unused objects.

Uploaded by

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

Unit 2 continue

Variable Argument (Varargs):

• The varrags allows the method to accept zero or muliple arguments.


Before varargs either we use overloaded method or take an array as
the method parameter but it was not considered good because it
leads to the maintenance problem.
• If we don't know how many argument we will have to pass in the
method, varargs is the better approach.
• Syntax of varargs:
return_type method_name(data_type... variableName){}
this keyword in java
• Keyword THIS is a reference variable in Java that refers to the current
object
• It can be used to refer instance variable of current class
• It can be used to invoke or initiate current class constructor
• It can be passed as an argument in the method call
• It can be passed as argument in the constructor call
• It can be used to return the current class instance
Understanding the problem without this keyword
Solution of the above problem by this keyword
Program where this keyword is not required
Final Keyword In Java

• The final keyword in java is used to restrict the user. The java final
keyword can be used in many context. Final can be:
• variable
• method
• class
1) Java final variable
• If you make any variable as final, you cannot change the value of final
variable(It will be constant).
2) Java final method
• If you make any method as final, you cannot override it
3) Java final class
• If you make any class as final, you cannot extend it.
Java Garbage Collection

• In java, garbage means unreferenced objects.


• Garbage Collection is process of reclaiming the runtime unused
memory automatically. In other words, it is a way to destroy the
unused objects.
• To do so, we were using free() function in C language and delete() in
C++. But, in java it is performed automatically. So, java provides better
memory management.
Advantage of Garbage Collection

• 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.
finalize() method

• The finalize() method is invoked each time before the object is


garbage collected. This method can be used to perform cleanup
processing. This method is defined in Object class as:
• Syntax:
• protected void finalize(){}
• The Garbage collector of JVM collects only those objects that are
created by new keyword. So if you have created any object without
new, you can use finalize method to perform cleanup processing
(destroying remaining objects).
gc() method

• The gc() method is used to invoke the garbage collector to perform


cleanup processing. The gc() is found in System and Runtime classes.
• public static void gc(){}  
• Garbage collection is performed by a daemon thread called Garbage
Collector(GC).
• This thread calls the finalize() method before object is garbage
collected.
• Neither finalization nor garbage collection is guaranteed.
Example of garbage collection in java

You might also like