When the variable value
is a primitive the finally block modification of the variable has no effect. However when value
is an reference the finally block modification of the variable takes effect. Can you please help why this happens?
StringBuilder value = new StringBuilder("abc");
StringBuilder get() {
try {
throw new IndexOutOfBoundsException();
} catch (IndexOutOfBoundsException e) {
return value;
} finally {
value = value.append("def");
}
}
int value = 10;
int get() {
try {
throw new IndexOutOfBoundsException();
} catch (IndexOutOfBoundsException e) {
return value;
} finally {
value = value + 10;
}
}