【发布时间】:2020-07-25 10:59:41
【问题描述】:
我发现一个现象:
public class TryTest {
public static void main(String[] args) {
System.out.println(test1());
}
public static int test1() {
try {
int a = 127;
return a;
} catch (Exception e) {
}finally {
System.out.println("I am finally");
}
return 0;
}
}
编译成.class:
public class TryTest {
public TryTest() {
}
public static void main(String[] args) {
System.out.println(test1());
}
public static int test1() {
try {
int a = 127;
byte var1 = a;
return var1;
} catch (Exception var5) {
} finally {
System.out.println("I am finally");
}
return 0;
}
}
为什么“int a”转换为“byte var1”?
是为了节省内存吗?
这不是不必要的吗?
我想知道编译器是如何处理这个的。
但我发现如果这样的代码:
public static int test3() {
int a = 1;
return a;
}
编译成.class:
public static int test3() {
int a = 1;
return a;
}
如果没有“try”,则不会编译成“byte”
【问题讨论】: