Found this question here
And I can't understand, why on first case it prints CoolReturn+1
and on second case CoolReturn
? How does it work?
Thanks
====================
What will be printed?
public void testFinally(){
System.out.println(setOne().toString());
}
protected StringBuilder setOne(){
StringBuilder builder=new StringBuilder();
try{
builder.append("Cool");
return builder.append("Return");
}finally{
builder.append("+1");
}
}
Answer: CoolReturn+1
A bit more difficult:
public void testFinally(){
System.out.println(setOne().toString());
}
protected StringBuilder setOne(){
StringBuilder builder=new StringBuilder();
try{
builder.append("Cool");
return builder.append("Return");
}finally{
builder=null; /* ;) */
}
}
Answer: CoolReturn
The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection. The finally block executes whether exception rise or not and whether exception handled or not. A finally contains all the crucial statements regardless of the exception occurs or not.
By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block run when control leaves a try statement.
No, we cannot write any statements in between try, catch and finally blocks and these blocks form one unit.
Exception handling ensures that the program's flow does not break when an exception occurs. For example, suppose a program contains many statements, and an exception occurs in the middle of executing some of them.
Consider
protected StringBuilder setOne(){
StringBuilder builder=new StringBuilder();
try{
builder.append("Cool");
return builder.append("Return");
}finally{
builder.append("+1");
}
}
as
protected StringBuilder setOne(){
StringBuilder builder=new StringBuilder();
try{
builder.append("Cool");
StringBuilder ret = builder.append("Return"); // 1
return ret; // 2
}finally{
builder.append("+1"); //3
}
}
line 1 is executed, the builder
is returned as result. Then line 3 is executed, and the builder
gets appended by +1
, then the ret
is returned which is a "reference" to the object referenced by builder
. The same is for the second case. Hope it is clear.
The first one:
The finally will always fire (assuming the machien doesn't crash or anything). So after the return, the finally block fires, and since it has a reference to the object "builder", it appends the extra token to it.
The second one:
The finally block fires just like before, but it sets the reference to builder to be null. The object still exists, because it still has a link to it.
When it does return builder.append("Return") a copy of the reference to the builder variable is pushed onto the stack. Then builder is set to null. The caller then pops the copy of the reference off the stack.
In the first example, in the finally block, you manipulate the string builder with append.
In the second example, in the finally block, you change your pointer to the string builder to be the null pointer, after you return the result from the append method. This does not modify the string builder that you formerly pointed to in any way.
EDIT: Looks like I got beat to it.
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