42. When does an object become subject to garbage collection? Describe using a Java example (show code).
43. What is the main difficulty in users performing their own free storage management?
44. What language allows users to perform their own free storage management?
45. What is overloading? Describe using java code showing an example of overloading.
46. Why would we desire to overload functions? Describe using a Java example (show code).
42.When does an object become subject to garbage collection? Describe using a Java example .
Object created inside a method : When a method
is called it goes inside the stack frame. When the method is popped
from the stack, all its members dies and if some objects were
created inside it then these objects becomes unreachable or
anonymous after method execution and thus becomes eligible for
garbage collection
.Example:
* Java program to demonstrate that
objects created inside a method will becomes
eligible for gc after method execution terminate */
class Test
{
// to store object name
String obj_name;
public Test(String obj_name)
{
this.obj_name = obj_name;
}
static void show()
{
//object t1 inside method becomes unreachable when show() removed
Test t1 = new Test("t1");
display();
}
static void display()
{
//object t2 inside method becomes unreachable when display() removed
Test t2 = new Test("t2");
}
// Driver method
public static void main(String args[])
{
// calling show()
show();
// calling garbage collector
System.gc();
}
@Override
/* Overriding finalize method to check which object
is garbage collected */
protected void finalize() throws Throwable
{
// will print name of object
System.out.println(this.obj_name + " successfully garbage collected");
}
}
Actually, it is the implementation that is system dependent. And until very recently, there was no unique solution to answer this question
43.The main difficulty in own frree space storage management is system dependent their we need to know all
low level details of system hardware like devide drivers for memory , hardware details of memory etc os used in system
44.the language which allow love level access to hardware ,allow us own free space storage management .
language like c ,c++ etc.
45.What is overloading?
Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both. Overloading is related to compile time (or static) polymorphism.
/ Java program to demonstrate working of method
// overloading in Java.
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
46.Why would we desire to overload functions?
the use of function overloading is to save the memory space,consistency and readabiliy.
It speeds up the execution of programme.
Function overloading is done for code re-usability, to save efforts, and also to save memory.
It helps app to load the class method based on the type of parameter
Code maintenance is easy.
public class Test
{
// Overloaded methods
public void fun(Integer i)
{
System.out.println("fun(Integer ) ");
}
public void fun(String name)
{
System.out.println("fun(String ) ");
}
// Driver code
public static void main(String [] args)
{
Test mv = new Test();
// This line causes error
mv.fun(null);
}
42. When does an object become subject to garbage collection? Describe using a Java example (show code). 43. What is the main difficulty in users performing their own free storage management? 44. What...