看注释,这样解释能看懂吧
public static void main(String[] args) {
StringBuilder sBuilder = new StringBuilder("AB");//1,添加了AB
String resulet = getString(sBuilder);
System.out.println(sBuilder);
System.out.println(resulet);
}
private static String getString(StringBuilder sb) {
try {
sb.append("CD");//2,添加了CD
return sb.toString();//3,返回sb,注意此时trycatch中先执行try中的return语句,此时已经返回了sb的值ABCD
} finally {
sb.append("EF");//再添加添加到sb上而不会改返回值
}
} |