Java转换实现 [英] Java casting implementation

查看:121
本文介绍了Java转换实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Java中使用转换,但有一个更具体的问题;

I know how to use casting in Java but have a more specific question; could you please explain to me how the casting works (in memory)?


  • 变量类型在上传和下传时如何变化?

  • How is the variable type changed upon upcasting and downcasting?

JVM如何知道将此方法发送到此对象是安全的?

How does the JVM know that it's safe to send this method to this object?

提前谢谢。

推荐答案


它可以在字节码级别工作真的在内存中

It works at byte code level not really in memory


如何在上传和下载时更改变量类型?

How the variable type is changed on upcasting and downcasting?

如果它是具有特殊字节码指令的原语,例如从长整数到整数,如:

If it is a primitive with an special bytecode instruction, for instance from long to integer as in:

long l = ...
int i = ( int ) l;

字节码是: l2i if引用 checkcast

The bytecode is: l2i if is a reference with the instruction checkcast


JVM如何知道时间可以安全地将此方法发送到此对象?

没有,它试图在运行时如果失败则抛出异常。

It doesn't, it tries to do it at runtime and if it fails throws an exception.

这是合法的:

String s = ( String ) new Date();

这篇关于Java转换实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆