会跑的东西,如果我让我的变量最后的更快? [英] Will things run quicker if I make my variables final?

查看:118
本文介绍了会跑的东西,如果我让我的变量最后的更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写为Android(Java)的。

I'm writing for Android (Java).

我声明了廉政局和float作为一个持续循环的一部分。

I'm declaring int's and float's as part of an ongoing loop.

他们中有些人并不需要声明之后被改变。

Some of them don't need to be changed after declaration.

如果我把它们都设置为最终宣布时,将东西跑得快?

If I set them all to final when declaring, will things run quicker?

谢谢大家。我实际上并没有指望它做任何改进,我只注意到,浏览各种大型项目的源代码后,这是很常见的。干杯

Thanks everyone. I didn't actually expect it to make any improvements, I just noticed, after browsing the source of various large projects, it was fairly common. Cheers

推荐答案

事情将不会运行更快。在最后关键字只是编译的时候语法糖。

Things will not run quicker. The final keyword is just compile time syntactic sugar.

如果它实际上是静态最后,那么你可以采取任何refernce的价值编译时计算和内联的好处。所以,用例如:

If it were actually static final, then you could take benefit of compiletime calculation and inlining of the value in any refernce. So, with for example:

private static final long ONE_WEEK_IN_MILLIS = 7 * 24 * 60 * 60 * 1000L;

public void foo(Date date) {
    if (date.getTiem() > System.currentTimeMillis() + ONE_WEEK_IN_MILLIS) {
        // No idea what to do here?
    }
}

,编译器将优化一个又一个,这样它结束这样的:

the compiler will optimize one and other so that it ends up like:

private static final long ONE_WEEK_IN_MILLIS = 604800000L;

public void foo(Date date) {
    if (date.getTiem() > System.currentTimeMillis() + 604800000L) {
        // No idea what to do here?
    }
}

如果你运行一个反编译器,你就自己看看吧。

If you run a decompiler, you'll see it yourself.

这篇关于会跑的东西,如果我让我的变量最后的更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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