Double and Double differences

Source: Internet
Author: User

When using double for logical calculation, it is found that the result of logical calculation differs from the expected result in individual cases.
Finding the wrong boxed basic type double using the lookup data is summarized as follows:

Differences between the base type and the boxed base type:

1. The basic type has only values, while the boxed base type has a different identity than its value. that is, the boxed base type (for example, double) is compared to its identity, rather than the function value, when the logical operation is performed.

eg

Double a = 100.1;

Double a = 100.1;

System.out.println ("Boxed base type comparison b>a?-1: (a==b?0:1):" + (b>a?-1: (a==b?0:1)));

Execution Result: Boxed base type comparison b>a?-1: (a==b?0:1): 1

The execution result is inconsistent with the expected result 0, so that when the boxed base type is logically operated, its identity participates in the operation.

The following changes are now available:

System.out.println ("Boxing base type conversion to base type comparison b>a?-1: (a==b?0:1):" + (A.doublevalue () >b.doublevalue ()?-1: ( A.doublevalue () ==b.doublevalue () 0:1)));

Execution Result: Boxing base type conversion to base type comparison b>a?-1: (a==b?0:1): 0

The function value of the boxed base type is logically calculated, and the execution result is the same as expected.

When using a basic type for logical operations, the problem does not exist because the basic type has no identity and only the function value.

eg

Double C = 100.1;
Double d = 100.1;
System.out.println ("Base type comparison:" + (c>d?-1: (c==d?0:1)));

The result of the execution is: basic type comparison: 0

Consistent with expected results

2, the basic type only has the function complete value, but the boxing basic type except its corresponding basic type all function value, also has a non-function value: null;

eg

Double a1 = null; Compilation does not pass

Double a2 = null; Compiled by

3, the basic type is usually more time and space than the basic type of boxing, because the basic type of boxing more than the basic type of identity and other properties, so it occupies more memory space than the basic type.

eg

Long beginTime = System.currenttimemillis ();
Long sum = 0L;
for (long i=0l;i<integer.max_value;i++) {
sum + = i;
}
Long endTime = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("The calculation time of the boxed primitive type long:" + (Endtime-begintime));

BeginTime = System.currenttimemillis ();
Long Sum_long = 0;
for (long i=0;i<integer.max_value;i++) {
Sum_long + = i;
}
EndTime = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Calculation time of the basic type long:" + (Endtime-begintime));

Execution Result:

Boxing base type Long's calculation time: 8753
Calculation time for the base type long: 1391

It can be judged that the basic type saves time and space compared to the boxed base type.

4, the interface should use boxing basic type, cannot use the basic type. such as:threadlocal<integer>, list<integer>, map<integer,object> and so on.

Also, the boxed base type should be used in the mapping file of the entity class, otherwise if the database field is NULL, an exception is thrown when the data is queried.

This article is from the "Dream Escape" blog, please be sure to keep this source http://pinmei.blog.51cto.com/7012784/1596062

Double and Double differences

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.