简体   繁体   中英

Passing many Strings in Java Methods

Looking for best advice on the following optimization of many calls:

myMethod("This is a test string with value: " + var + ".");

The program has many calls with this type of behavior. Ie: this type of method is called thousands of times over of the course of program usage.

Should a cache of some sorts be built to max this optimization of string usage?

myMethod(cache.get(Cache.TEST_STRING_000001) + " + var + ".);

Tips welcomed and proper instruction on best practices and efficieny is also welcomed.

No. That is completely useless. Each time this code is executed, the same String instance is used for "This is a test string with value: " .

Even more, if the same String literal is used in 10 different classes, a unique instance is used in all classes.

Your code would be less readable, especially with such a meaningless name as TEST_STRING_000001 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM