Java 转换会引入开销吗?为什么? [英] Does Java casting introduce overhead? Why?

查看:25
本文介绍了Java 转换会引入开销吗?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们将一种类型的对象转换为另一种类型时是否有任何开销?还是编译器只是解决了所有问题而没有运行时的成本?

Is there any overhead when we cast objects of one type to another? Or the compiler just resolves everything and there is no cost at run time?

这是一般的事情,还是有不同的情况?

Is this a general things, or there are different cases?

例如,假设我们有一个 Object[] 数组,其中每个元素可能有不同的类型.但是我们总是可以肯定地知道,比如说,元素 0 是一个 Double,元素 1 是一个字符串.(我知道这是一个错误的设计,但让我们假设我必须这样做.)

For example, suppose we have an array of Object[], where each element might have a different type. But we always know for sure that, say, element 0 is a Double, element 1 is a String. (I know this is a wrong design, but let's just assume I had to do this.)

Java 的类型信息在运行时是否仍然保留?或者编译后什么都忘记了,如果我们做(Double)elements[0],我们就跟着指针把这8个字节解释为double,不管是什么?

Is Java's type information still kept around at run time? Or everything is forgotten after compilation, and if we do (Double)elements[0], we'll just follow the pointer and interpret those 8 bytes as a double, whatever that is?

我不太清楚 Java 中的类型是如何完成的.如果您对书籍或文章有任何建议,也谢谢.

I'm very unclear about how types are done in Java. If you have any reccommendation on books or article then thanks, too.

推荐答案

有两种类型的转换:

隐式转换,当你从一个类型转换到一个更宽的类型时,这是自动完成的,没有开销:

Implicit casting, when you cast from a type to a wider type, which is done automatically and there is no overhead:

String s = "Cast";
Object o = s; // implicit casting

显式转换,当您从较宽的类型转换为较窄的类型时.对于这种情况,您必须像这样显式地使用强制转换:

Explicit casting, when you go from a wider type to a more narrow one. For this case, you must explicitly use casting like that:

Object o = someObject;
String s = (String) o; // explicit casting

在第二种情况下,运行时存在开销,因为必须检查这两种类型,并且如果强制转换不可行,JVM 必须抛出 ClassCastException.

In this second case, there is overhead in runtime, because the two types must be checked and in case that casting is not feasible, JVM must throw a ClassCastException.

取自 JavaWorld:成本铸造

Casting 用于在类型——在引用类型之间特别是对于铸件的类型我们感兴趣的操作这里.

Casting is used to convert between types -- between reference types in particular, for the type of casting operation in which we're interested here.

Upcast 操作(也称为Java 中的扩展转换语言规范)转换一个对祖先的子类引用类参考.这个铸造操作通常是自动的,因为它总是安全的并且可以由编译器直接实现.

Upcast operations (also called widening conversions in the Java Language Specification) convert a subclass reference to an ancestor class reference. This casting operation is normally automatic, since it's always safe and can be implemented directly by the compiler.

Downcast 操作(也称为Java 中的缩小转换语言规范)转换一个祖先类对子类的引用参考.这个铸造操作产生执行开销,因为 Java要求演员在运行时以确保它是有效的.如果引用的对象不是任一目标类型的实例该类型的演员表或子类,不允许尝试的演员表并且必须抛出一个java.lang.ClassCastException.

Downcast operations (also called narrowing conversions in the Java Language Specification) convert an ancestor class reference to a subclass reference. This casting operation creates execution overhead, since Java requires that the cast be checked at runtime to make sure that it's valid. If the referenced object is not an instance of either the target type for the cast or a subclass of that type, the attempted cast is not permitted and must throw a java.lang.ClassCastException.

这篇关于Java 转换会引入开销吗?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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