What is the effect of not catching the value of the method that returns a value?
Does it develop complications like memory issues if the return value was not caught.
Example code snippets:
//reference types
public Object[] thismethodreturnsvalue(){
return new Object[]{new Object(),new Object(),new Object()};
}
//primitive types
public int thismethodreturnsint(){
return -1;
}
public static void main(String a[]){
thismethodreturnsvalue();
thismethodreturnsint();
}
You need to return something in the case that return fill; is never executed. That can happen if height or width is less than 1 . In this case, it doesn't make sense to enter a for loop (or even 2 loops) if all you're going to do is return fill .
What is a return statement in Java? In Java programming, the return statement is used for returning a value when the execution of the block is completed. The return statement inside a loop will cause the loop to break and further statements will be ignored by the compiler.
The “missing return statement” message occurs when a method does not have a return statement. Each method that returns a value (a non-void type) must have a statement that literally returns that value so it can be called outside the method.
length . == is an operator which returns true when its left and right side are equal and false otherwise.
1. What is the effect of not catching the value of the method that returns a value?
No effect really.
The return value will be computed, and if it was created on the heap (as in your first example), it will be eligible for garbage collection right away.
In your second example, the return value will end up on the stack and will simply be discarded after the method has returned.
2. Does it develop complications like memory issues if the return value was not caught.
No, not really. As I said above, it will be garbage collected.
Here is a bytecode dump of what happens at the call site:
public class Test {
public static int testMethod() {
return 5;
}
public static void main(String[] args) {
testMethod();
}
}
... corresponding bytecode:
public static int testMethod();
Code:
0: iconst_5
1: ireturn
public static void main(java.lang.String[]);
Code:
0: invokestatic #2;
3: pop // return value popped immediately.
4: return
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With