会跑的东西,如果我让我的变量最后的更快?我的、更快、我让、变量

2023-09-07 15:27:32 作者:命比屌硬°

我在写为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.