Does not storing or using what "sum" returns potentially cause a memory leak?
public int sum(int a, int b){
System.out.println("total is: "+(a+b));
return a+b;
}
sum(2,3);
Does not storing or using what "sum" returns potentially cause a memory leak?
public int sum(int a, int b){
System.out.println("total is: "+(a+b));
return a+b;
}
sum(2,3);
No, it does not. As it is a primitive value, it lives on the stack, and no heap memory is allocated for it.
But let's suppose, for the sake of demonstration, you are returning an object and not a primitive value (ex. return new Integer(a+b)
). There would still be no memory leak, as the object being returned would have no references to it, and thus would be subject to garbage collection.
No, Java has the garbage collector to help manage the memory. Hope this could help. What is the garbage collector in Java?