在Java中强制转换需要多少处理和内存使用? [英] How much processing and memory use does casting take in Java?

查看:73
本文介绍了在Java中强制转换需要多少处理和内存使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑是否最好有两个指针,每个对象的子类和上一个指针一个,还是应该只使用强制转换.

I am considering whether it is better to have two pointers, one for each object sub class and super, or whether I should just use casting.

这会使用多少系统资源:

How much system resources does this use:

objectName.functionOne();
((SubClass) objectName).functionOther();

比以下更好吗?

SuperClass objectA = (SuperClass) getSameInstance();
SubClass objectB = getSameInstance();
objectA.functionOne();
objectB.functionOther();

基本上,我的主要问题是与转换有关的资源,而不是额外的指针.看来我可以保存几行强制转换,例如:

Basically, my main question is about the resources used with casting, versus making an extra pointer. It seems like I could save several in line casts, such as:

((SubClass) objectName).functionOther();

但是,这值得吗?

谢谢,
严重

Thanks,
Grae

我的问题有一些不清楚的部分.基本上,我有一个贯穿整个大型函数的超类.它适用于三个子类.一些超类正在按我的意愿工作.但是,在一些地方,我不得不使用三个不同子类之一中的函数,这是一个障碍.一个仅在子类之一中的函数.

There were some unclear parts to my question. Basically, I have a super class that I am using through out a large function. It works with three subclasses. Some the super class is working as I would like. However, I hit a road block in a few places where I have to use a function from one of the three different subclass; a function that is only in one of the subclasses.

我可以拥有:

SuperClass instanceRef;
SubClass instanceRef2;

instanceRef.etc()
instanceRef.etc2()
instanceRef.etc3()
instanceRef2.specialSubClassOnlyCall();
instanceRef2.specialSubClassOnlyCall2();

或者我可以拥有:

SuperClass instanceRef;

instanceRef.etc()
instanceRef.etc2()
instanceRef.etc3()
((SpecialSubClass)instanceRef).specialSubClassOnlyCall();
((SpecialSubClass)instanceRef).specialSubClassOnlyCall2();

但是,我不知道哪个更有效.

However, I don't know which is more efficient.

下面是一个示例,向您展示我在说什么:

Here is an example to show you what I am talking about:

class Shape
Triangle extends Shape
Square extends Shape
Circle extends Shape
Cube extends Shape

两个指针的示例:(下方有一个额外的指针.)

The Two Pointer Example: (Downside an extra pointer.)

Shape pointer1 = (Shape) getSomeRandomShape();
Cube pointer2 = null;

pointer1.getWidth();
pointer1.getHeight();
pointer1.generalShapeProp();
pointer1.generalShapeProp2();
pointer1.generalShapeProp3();

if(sure_its_cube)
{
   pointer2 = (Cube) pointer1;
   pointer2.getZAxis();
   pointer2.getOtherCubeOnlyThing();
   pointer2.getOtherCubeOnlyThing2();
   pointer2.getOtherCubeOnlyThing3();
   pointer2.getOtherCubeOnlyThing4();
}

或者我可以这样做. (下面是一堆演员.)

Or I could do it this way. (Downside a bunch of casts.)

Shape pointer1 = (Shape) getSomeRandomShape();

pointer1.getWidth();
pointer1.getHeight();
pointer1.generalShapeProp();
pointer1.generalShapeProp2();
pointer1.generalShapeProp3();

if(sure_its_cube)
{
   ((Cube)pointer1).getZAxis();
   ((Cube)pointer1).getOtherCubeOnlyThing();
   ((Cube)pointer1).getOtherCubeOnlyThing2();
   ((Cube)pointer1).getOtherCubeOnlyThing3();
   ((Cube)pointer1).getOtherCubeOnlyThing4();
}

那么5个强制转换是否比1个额外的指针差?那是六个演员,或二十个?一个比指针差吗?

So is five casts worse than one extra pointer? What is it was six casts, or 20? Is one cast worse than the pointer.

Grae

推荐答案

在您的代码段中,似乎getSameInstance()返回了SubClass.在这种情况下,最简单的解决方案是

It seems from your code snippet that getSameInstance() returns a SubClass. In this case the simplest solution is

SubClass objectB = getSameInstance();
objectB.functionOne();
objectB.functionOther();

无演员,无后顾之忧:-)

No casts, no worries :-)

正如其他人正确指出的那样,主要关注点应该是代码的可读性和简单性,而不是性能. 仅在知道自己需要并且已证明(使用事件探查器)代码中瓶颈所在的地方进行优化.即使在这种情况下,微优化在Java中也很少有用.

As others have rightly noted, the primary concern should be code readability and simplicity, not performance. Optimize only when you know that you need to, and have proven (using a profiler) where the bottleneck is within your code. Even in this case, micro-optimization is rarely useful in Java.

附加说明:如果该方法很繁重(在这种情况下,您实际上使代码变慢而不是变快),或者有副作用(在这种情况下,程序的行为可能明显改变).

An additional note: calling the same method twice can be problematic if the method is heavy (in which case you are effectively making your code slower, not faster), or has side effects (in which case the behaviour of your program may noticeably change).

在代码段的这一行中顺便说一句

And btw in this line of your code snippet

SuperClass objectA = (SuperClass) getSameInstance();

绝对没有必要将返回值上载到SuperClass-超类引用始终可以指向子类对象. (我想编译器还是会忽略这样的向上转换,因此字节码没有区别.)

there is absolutely no need to upcast the returned value to SuperClass - a superclass reference can always point to a subclass object. (I guess that such an upcast would be omitted by the compiler anyway, so it makes no difference in the bytecode.)

您仍然没有发布我要求的方法的声明.但是根据您的解释,我认为它应该看起来像

You still haven't published the declaration of the method I asked for. But from your explanation I presume it should look something like

public Shape getSomeRandomShape();

这正确吗?

这将意味着您的第二个原始代码段(带有getSameInstance()调用)不正确-强制转换应采用相反的方式.您设法使我感到困惑,所以得到了一个令人困惑的答案:-)

This would btw mean that your 2nd original code snippet (with the getSameInstance() call) is incorrect - the casts should be the opposite way. You managed to confuse me with it, so you got a confusing answer :-)

在最新示例中,您也无需将返回值强制转换为(Shape),因为它已经是Shape.强制转换只是使您的代码混乱.同样,UPDATE 2的第二个示例中的许多强制转换使代码难以阅读.我希望使用具有两个引用的第一个版本( not 指针btw-Java没有指针).

Also in the latest examples, you don't need to cast the returned value to a (Shape), since it is already a Shape. The cast is just cluttering your code. Similarly, the many casts in the 2nd example of your UPDATE 2 are making the code hard to read. I would prefer the first version, with two references (not pointers btw - Java has no pointers).

在这里,我只关心代码的可读性-正如其他人已经指出的那样,您不应该真的在浪费时间思考转换的成本.只需专注于使您的代码尽可能简单易读.您很可能永远不会注意到上面显示的两个代码片段的性能之间的微小差异(除非它们在紧密循环中执行了数百万次-但是,我想,JIT会优化无论如何都将其丢掉).但是,对于一个刚接触此代码的人或一个已经忘记详细信息的人(可能在6个月内可能成为您),您会发现理解一个代码段与另一个代码段所花费的时间有所不同.

And I care only about code readability here - as others have already noted, you should really, really not waste your time thinking about the cost of casts. Just focus on making your code as simple and readable as possible. You would most likely never ever going to notice the tiniest bit of difference between the performance of the two code snippets you show above (unless maybe if they are executed in a tight loop millions of times - but then, I guess, the JIT would optimize the casts away anyway). However, you will notice the difference it takes in time to understand one code snippet versus the other, for someone who is new to this code - or someone who has forgotten the details already, which could be you in about 6 months.

这篇关于在Java中强制转换需要多少处理和内存使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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