测试try-eturn,finally里++
使用jdk8进行测试
public class Sample {
public static void main(String[] args) throws Throwable {
int retInt = testInt();
System.out.println("result:" + retInt);
System.out.println("----------------");
String retStr = testString();
System.out.println("result:" + retStr);
System.out.println("----------------");
Map<String, String> retMap = testMap();
System.out.println("result:" + retMap.get("test"));
}
private static int testInt() {
int i = 1;
try {
i++;
System.out.println("try:" + i);
return i;
} finally {
i++;
System.out.println("finally:" + i);
}
}
private static String testString() {
String i = "test";
try {
i = "test1";
System.out.println("try:" + i);
return i;
} finally {
i = "test2";
System.out.println("finally:" + i);
}
}
private static Map<String, String> testMap() {
Map<String, String> map = new HashMap<>();
try {
map.put("test", "test1");
System.out.println("try:" + map.get("test"));
return map;
} finally {
map.put("test", "test2");
System.out.println("finally:" + map.get("test"));
}
}
}
输出
try:2
finally:3
result:2
----------------
try:test1
finally:test2
result:test1
----------------
try:test1
finally:test2
result:test2
查看反编译后的代码
public class Sample {
public Sample() {
}
public static void main(String[] args) throws Throwable {
int retInt = testInt();
System.out.println("result:" + retInt);
System.out.println("----------------");
String retStr = testString();
System.out.println("result:" + retStr);
System.out.println("----------------");
Map<String, String> retMap = testMap();
System.out.println("result:" + (String)retMap.get("test"));
}
private static int testInt() {
int i = 1;
int var1;
try {
++i;
System.out.println("try:" + i);
var1 = i;
} finally {
++i;
System.out.println("finally:" + i);
}
return var1;
}
private static String testString() {
String i = "test";
String var1;
try {
i = "test1";
System.out.println("try:" + i);
var1 = i;
} finally {
i = "test2";
System.out.println("finally:" + i);
}
return var1;
}
private static Map<String, String> testMap() {
HashMap map = new HashMap();
HashMap var1;
try {
map.put("test", "test1");
System.out.println("try:" + (String)map.get("test"));
var1 = map;
} finally {
map.put("test", "test2");
System.out.println("finally:" + (String)map.get("test"));
}
return var1;
}
}
结论:
java编译时做了代码优化,使用var1变量替换了原来的返回值。
测试try-return,finally里++后又return
public class Sample {
public static void main(String[] args) throws Throwable {
int retInt = testInt();
System.out.println("result:" + retInt);
System.out.println("----------------");
String retStr = testString();
System.out.println("result:" + retStr);
System.out.println("----------------");
Map<String, String> retMap = testMap();
System.out.println("result:" + retMap.get("test"));
}
private static int testInt() {
int i = 1;
try {
i++;
System.out.println("try:" + i);
return i;
} finally {
i++;
System.out.println("finally:" + i);
return i;
}
}
private static String testString() {
String i = "test";
try {
i = "test1";
System.out.println("try:" + i);
return i;
} finally {
i = "test2";
System.out.println("finally:" + i);
return i;
}
}
private static Map<String, String> testMap() {
Map<String, String> map = new HashMap<>();
try {
map.put("test", "test1");
System.out.println("try:" + map.get("test"));
return map;
} finally {
map.put("test", "test2");
System.out.println("finally:" + map.get("test"));
return map;
}
}
}
输出
try:2
finally:3
result:3
----------------
try:test1
finally:test2
result:test2
----------------
try:test1
finally:test2
result:test2
查看反编译后的代码
public class Sample {
public Sample() {
}
public static void main(String[] args) throws Throwable {
int retInt = testInt();
System.out.println("result:" + retInt);
System.out.println("----------------");
String retStr = testString();
System.out.println("result:" + retStr);
System.out.println("----------------");
Map<String, String> retMap = testMap();
System.out.println("result:" + (String)retMap.get("test"));
}
private static int testInt() {
int i = 1;
try {
++i;
System.out.println("try:" + i);
} finally {
++i;
System.out.println("finally:" + i);
return i;
}
}
private static String testString() {
String i = "test";
try {
i = "test1";
System.out.println("try:" + i);
} finally {
i = "test2";
System.out.println("finally:" + i);
return i;
}
}
private static Map<String, String> testMap() {
HashMap map = new HashMap();
try {
map.put("test", "test1");
System.out.println("try:" + (String)map.get("test"));
} finally {
map.put("test", "test2");
System.out.println("finally:" + (String)map.get("test"));
return map;
}
}
}
结论:
java编译时做了代码优化,只使用finally里的return。
|