Use the Java base type object class sparingly

Source: Internet
Author: User

If you can use a basic type, use the base type as much as possible and avoid using the corresponding object class. such as: general first recommended int, long, float, double, as far as possible to use the integer, double, float, double type. Because in some cases, Java will automatically box and unboxing operations, resulting in program performance issues.

In my local computer running the following two programs, there is a large performance difference:

public class Longtest {public static void main (string[] args) {long starttime=system.currenttimemillis ();        Long sum=0l;        for (long i=0;i<integer.max_value;i++) {sum+=i;          } System.out.println (sum);        Long Endtime=system.currenttimemillis ();    SYSTEM.OUT.PRINTLN ("Time Consuming:" + (Endtime-starttime) + "millisecond"); }}

The program runs time: 8458 milliseconds;

public class LongTest1 {public static void main (string[] args) {long starttime=system.currenttimemillis ();        Long sum=0l;        for (long i=0;i<integer.max_value;i++) {sum+=i;              } System.out.println (sum);        Long Endtime=system.currenttimemillis ();    SYSTEM.OUT.PRINTLN ("Time Consuming:" + (Endtime-starttime) + "millisecond"); }}

The program runs time: 1364 milliseconds;

Thus, the basic type of object class is less than the basic type of performance, in the case of unnecessary, as far as possible to use the basic type of code to write.

Use the Java base type object class sparingly

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.