Integerutils: A Tool class for integer operations

Source: Internet
Author: User

Write by yourself recentlyProgramTo improve the performance of the program.

 

A basic idea is to reduce the creation of objects. Because our program requires a lot of integer operations, including integer. tostring (), integer. tohexstring () and so on. In theory, each time you call these functions, an integer is parsed and a string is generated. Therefore, I write a class to cache the results. I don't have to calculate the result for the second call. 

 

BelowCodePublished in the public domain, you can use them freely.

/** <Br/> * integer utils, cached batch results of tostring/tohexstring to get better performance <p> <br/> * Java 1.4 compatible <br/> * @ author henix [http://blog.csdn.net/shell_picker/> */<br/> public class integerutils {</P> <p> static final int max_cached= 2048; </P> <p> Private Static integer [] cache = new integer [max_cached]; <br/> Private Static string [] cachedstrings = new string [max_cached]; <br/> Private Static string [] cachedhexstrings = new string [max_cached]; </P> <p>/** <br/> * implement the same logic as integer. valueof (INT) in Java 1.5. <br/> * @ Param I <br/> * @ return <br/> */<br/> Public static integer getinteger (int I) {<br/> if (I >= 0 & I <max_cached) {<br/> synchronized (cache) {<br/> If (Cache [I] = NULL) {<br/> cache [I] = new INTEGER (I ); <br/>}< br/> return cache [I]; <br/>}else {<br/> return New INTEGER (I ); <br/>}</P> <p> Public static string tostring (int I) {<br/> if (I >= 0 & I <max_cached) {<br/> synchronized (cachedstrings) {<br/> If (cachedstrings [I] = NULL) {<br/> cachedstrings [I] = integer. tostring (I); <br/>}< br/> return cachedstrings [I]; <br/>} else {<br/> return integer. tostring (I); <br/>}</P> <p> Public static string tohexstring (int I) {<br/> if (I >= 0 & I <max_cached) {<br/> synchronized (cachedhexstrings) {<br/> If (cachedhexstrings [I] = NULL) {<br/> cachedhexstrings [I] = integer. tohexstring (I); <br/>}< br/> return cachedhexstrings [I]; <br/>}else {<br/> return integer. tohexstring (I ); <br/>}</P> <p>/** <br/> * parseint that never throws exceptions <br/> * @ Param STR <br/> * @ return <br/> */<br/> Public static int parseint (string Str) {<br/> try {<br/> return integer. parseint (STR); <br/>} catch (exception e) {<br/> return 0; <br/>}</P> <p> Public static int parseint (string STR, int Radix) {<br/> try {<br/> return integer. parseint (STR, Radix); <br/>}catch (exception e) {<br/> return 0; <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.