String. valueOf conversion and toString conversion of Java, empty String + Type Variable conversion and encapsulation class (Integer)

Source: Internet
Author: User

1. Empty string + Type Variable Conversion

 i=20=""+i;

This method actually goes through two steps. First I. ToString () is used to convert I to a string, and then the addition operation is performed. Here the toString mechanism of java is used for conversion.

2. String. valueOf type conversion

 i=20=String.valueOf(i);

Check the source code and find that this method actually uses the toString method of the encapsulation class (Integer) for conversion.

   String valueOf(     }

 

3. Use the toString () method of the encapsulation class

 

Integer i=20=i.toString();

 

= Random (= runtimes = 1000000 range = 50 startTime = System. currentTimeMillis (); (I = 0; I <runtimes; I ++ = endTime = System. currentTimeMillis (); "use String. valueOf program running time: "+ (endTime-startTime) +" ms "startTimeToString = System. currentTimeMillis (); (I = 0; I <runtimes; I ++ = "" + endTimeString = System. currentTimeMillis (); System. out. println ("use (empty string to convert + type variable) running time:" + (endTimeString-startTimeToString) + "ms" = 0 startTimeToString1 = System. currentTimeMillis (); (I = 0; I <runtimes; I ++ = endTimeString2 = System. currentTimeMillis (); "use Integer toString runtime:" + (endTimeString2-startTimeToString1) + "ms"

Running time with String. valueOf: 87 ms
Program running time: 245 ms using (empty String Conversion + type variable)
The running time of the toString using Integer is 77 ms.

 

 

After a simple test, it is found that the (empty String Conversion + type variable) conversion is more than twice slower than the String. valueOf and Integer toString conversion.

I personally understand that the efficiency of the conversion using (empty String Conversion + type variable) method is slow because this method has actually gone through two steps, first I. toString () converts I to a string and then performs addition operations on the string. Because the string is unchangeable, a new memory space is required to store the new string, the string addition operation affects the efficiency.

 

 

 

Related Article

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.