开发者

Is it faster to access a property file or static constants from a class file?

开发者 https://www.devze.com 2022-12-28 03:16 出处:网络
I develop a webservice application in a tomcat container, I have a lot of properties for the webapp like constants, error messa开发者_如何学运维ges and so on.

I develop a webservice application in a tomcat container, I have a lot of properties for the webapp like constants, error messa开发者_如何学运维ges and so on.

What is the better and faster way to?


The answer, as always, is profile it.

In general, this is the sort of micro-optimization that almost certainly won't matter. Feel free to profile it and see, though.


Generally static constants; IO activity will always be slower.

However, hard-coding values means that the app will need to be recompiled if the value has to change. It's your call as a developer as to whether the values will ever need to be changed outside of a software release.


As Bozho says, in terms of raw speed, you're going to have trouble beating a public static final. But speed isn't everything. If you need to worry about localization at all, for instance, a properties file would probably be better, although you might want to look at a ResourceBundle instead.


public static final String SOME_CONSTANT = "contstantValue";

This makes the compiler inline the value, so you have nothing happening at runtime.

You can also store the values anywhere (.properties file) and load them in a static initializer block.

0

精彩评论

暂无评论...
验证码 换一张
取 消