博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java c 转换_由javac完成的任何类型的转换?
阅读量:6269 次
发布时间:2019-06-22

本文共 2376 字,大约阅读时间需要 7 分钟。

对于在运行时不需要测试的转换,编译器可能会进行一些优化以避免在运行时进行转换。

我建议阅读JLS Chapter 5. Conversions and Promotions以了解有关在运行时需要测试的转换类型的更多信息。

Example 5.0-1. Conversions at Compile-time and Run-time

A conversion from type Object to type Thread requires a run-time check to make sure that the run-time value is actually an instance of class Thread or one of its subclasses; if it is not, an exception is thrown.

A conversion from type Thread to type Object requires no run-time action; Thread is a subclass of Object, so any reference produced by an expression of type Thread is a valid reference value of type Object.

A conversion from type int to type long requires run-time sign-extension of a 32-bit integer value to the 64-bit long representation. No information is lost.

A conversion from type double to type long requires a nontrivial translation from a 64-bit floating-point value to the 64-bit integer representation. Depending on the actual run-time value, information may be lost.5.1.6. Narrowing Reference Conversion:

Such conversions require a test at run-time to find out whether the actual reference value is a legitimate value of the new type. If not, then a ClassCastException is thrown.

5.1.8. Unboxing Conversion;转换在运行时进行。

另见:5.5.3. Checked Casts at Run-time

例如,确定转换发生的时间并不容易:

public class Main {

private static class Child extends Parent{

public Child() {

}

}

private static class Parent {

public Parent() {

}

}

private static Child getChild() {

Parent o = new Child();

return (Child) o;

}

public static void main(final String[] args) {

Child c = getChild();

}

}javap -c Main给出的结果是:

public class Main extends java.lang.Object{

public Main();

Code:

0: aload_0

1: invokespecial #1; //Method java/lang/Object."":()V

4: return

public static void main(java.lang.String[]);

Code:

0: invokestatic #4; //Method getChild:()LMain$Child;

3: astore_1

4: return

}如果将方法声明更改为public static Child getChild(),结果为:

public class Main extends java.lang.Object{

public Main();

Code:

0: aload_0

1: invokespecial #1; //Method java/lang/Object."":()V

4: return

public static Main$Child getChild();

Code:

0: new #2; //class Main$Child

3: dup

4: invokespecial #3; //Method Main$Child."":()V

7: astore_0

8: aload_0

9: checkcast #2; //class Main$Child

12: areturn

public static void main(java.lang.String[]);

Code:

0: invokestatic #4; //Method getChild:()LMain$Child;

3: astore_1

4: return

}您看到只是更改访问者,可能会对可能的优化产生很大影响。

转载地址:http://uxspa.baihongyu.com/

你可能感兴趣的文章
JQuery datepicker 用法
查看>>
golang(2):beego 环境搭建
查看>>
天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十)态势标绘模块
查看>>
程序员社交宝典
查看>>
ABP理论学习之MVC控制器(新增)
查看>>
Netty中的三种Reactor(反应堆)
查看>>
网页内容的html标签补全和过滤的两种方法
查看>>
前端源码安全
查看>>
【CodeForces 618B】Guess the Permutation
查看>>
【转】如何实现一个配置中心
查看>>
Docker —— 用于统一开发和部署的轻量级 Linux 容器【转】
查看>>
Threejs 官网 - Three.js 的图形用户界面工具(GUI Tools with Three.js)
查看>>
Atitit.Java exe bat 作为windows系统服务程序运行
查看>>
session的生命周期
查看>>
数据库的本质、概念及其应用实践(二)
查看>>
iOS开发多线程--(NSOperation/Queue)
查看>>
php的ajax简单实例
查看>>
maven常用构建命令
查看>>
C#:关联程序和文件
查看>>
推荐科研软件
查看>>