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