Java 显式转换为相同类型
对相同类型的显式转换是否会影响性能,或者它们是否会被编译器过滤而永远不会到达字节码 例如:Java 显式转换为相同类型,java,performance,Java,Performance,对相同类型的显式转换是否会影响性能,或者它们是否会被编译器过滤而永远不会到达字节码 例如: int x = 3; int y = (int) x; Sun称之为 --引自链接-- 任何类型都允许从一个类型转换为同一类型 这似乎微不足道,但它有两个实际后果。第一, 始终允许表达式具有所需的类型 从开始,因此允许简单陈述的规则,每个表达式 如果只是一个微不足道的身份转换,则要进行转换。 其次,它意味着允许一个程序包含 为了清晰起见,需要冗余的cast操作员 在此类上运行javap-c: publi
int x = 3;
int y = (int) x;
Sun称之为
--引自链接--
任何类型都允许从一个类型转换为同一类型
这似乎微不足道,但它有两个实际后果。第一,
始终允许表达式具有所需的类型
从开始,因此允许简单陈述的规则,每个表达式
如果只是一个微不足道的身份转换,则要进行转换。
其次,它意味着允许一个程序包含
为了清晰起见,需要冗余的cast操作员
在此类上运行javap-c:
public class SameTypeCastsDemo {
public static void withoutCasts() {
int x = 2;
int y = x;
System.out.println(y);
}
public static void withCast() {
int x = 2;
int y = (int) x;
System.out.println(y);
}
}
显示字节码看起来相同:
public static void withoutCasts();
Code:
0: iconst_2
1: istore_0
2: iload_0
3: istore_1
4: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
7: iload_1
8: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
11: return
public static void withCast();
Code:
0: iconst_2
1: istore_0
2: iload_0
3: istore_1
4: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
7: iload_1
8: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
11: return
更新:使用非基本体对象引用:
public class SameTypeCastsDemo {
Integer x;
Integer y;
public SameTypeCastsDemo(Integer x, Integer y) {
this.x = x;
this.y = y;
}
public void print() {
System.out.println(y);
}
public static void withoutCasts() {
SameTypeCastsDemo x = new SameTypeCastsDemo(2, 3);
SameTypeCastsDemo y = x;
y.print();
}
public static void withCast() {
SameTypeCastsDemo x = new SameTypeCastsDemo(2, 3);
SameTypeCastsDemo y = (SameTypeCastsDemo) x;
y.print();
}
}
javap-c SameTypeCastsDemo:
public static void withoutCasts();
Code:
0: new #6; //class SameTypeCastsDemo
3: dup
4: iconst_2
5: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
8: iconst_3
9: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
12: invokespecial #8; //Method "<init>":(Ljava/lang/Integer;Ljava/lang/Integer;)V
15: astore_0
16: aload_0
17: astore_1
18: aload_1
19: invokevirtual #9; //Method print:()V
22: return
public static void withCast();
Code:
0: new #6; //class SameTypeCastsDemo
3: dup
4: iconst_2
5: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
8: iconst_3
9: invokestatic #7; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
12: invokespecial #8; //Method "<init>":(Ljava/lang/Integer;Ljava/lang/Integer;)V
15: astore_0
16: aload_0
17: astore_1
18: aload_1
19: invokevirtual #9; //Method print:()V
22: return
publicstaticvoidwithoutcasts();
代码:
0:新#6//类SameTypeCastsDemo
3:dup
4:iconst_2
5:invokestatic#7//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
8:iconst_3
9:invokestatic#7//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
12:特别是#8//方法“”:(Ljava/lang/Integer;Ljava/lang/Integer;)V
15:astore_0
16:aload_0
17:astore_1
18:aload_1
19:invokevirtual#9//方法打印:()V
22:返回
公共静态void with cast();
代码:
0:新#6//类SameTypeCastsDemo
3:dup
4:iconst_2
5:invokestatic#7//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
8:iconst_3
9:invokestatic#7//方法java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
12:特别是#8//方法“”:(Ljava/lang/Integer;Ljava/lang/Integer;)V
15:astore_0
16:aload_0
17:astore_1
18:aload_1
19:invokevirtual#9//方法打印:()V
22:返回
为什么需要这样做?@HarryJoy我只是想知道编译器在这种奇怪的情况下的行为,因为它甚至没有发出警告。我真的写了对话吗?哈哈!谢谢你的编辑。我能假设这也适用于对象引用吗?