总是使用Final吗? [英] Always Use Final?

查看:96
本文介绍了总是使用Final吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读到做一些最终的事情,然后在循环中使用它会带来更好的性能,但是对所有事情都有利吗?我在很多地方都没有循环,但是我将final添加到局部变量中.它会使速度变慢还是仍然很好?

I have read that making something final and then using it in a loop will bring better performance, but is it good for everything? I have lots of places where there isnt a loop but I add final to the local variables. Does it make it slower or is it still good?

在某些地方,我有一个全局变量final(例如android paint),这是否意味着在循环使用它时不必将其设置为局部final?

Also there are some places where I have a global variable final (e.g. android paint), does it mean I don't have to make it a local final when using it in loops?

推荐答案

您应该考虑的第一件事是:我编写此代码最简单,最清晰的方法是什么.通常,这种方法效果不错.

The first thing you should consider is; What is the simplest and clearest way I can write this code. Often this performs well.

final局部变量不太可能对性能产生很大影响.当您使用长方法时,它们可以帮助澄清,但我建议分手方法是一种更好的方法.

final local variables is unlikely to affect performance much. They can help clarity when you have long methods, but I would suggest breaking up method is a better approach.

final字段可能会在很小程度上影响性能,但使其成为final的更好理由是要明确表明此字段永不更改(这也有助于JIT)

final fields can affect performance to small degree, but a better reason to make it final is to make it clear that this field never changes (which also helps the JIT)

这篇关于总是使用Final吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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