Java zero foundation entry series – the big value in Day9 Java

What is a large value? Think with your toes. Of course, it is a "large" value (233). There are two classes used to represent large values in Java, BigInteger and BigDecimal. How large can they represent? In theory, they can represent any length, any precision and how large they want.

Why use large values? The reason is very simple. When the precision of integers and floating-point numbers cannot meet the requirements, larger or higher precision types need to be used. At this time, large values came into being.

The value range of short is - 32768 ~ 32767, occupying 2 bytes (- the 15th power of 2 to the 15th power of 2 - 1).

The value range of int is (- 2147483648 ~ 2147483647), occupying 4 bytes (- 31st power of 2 to 31st power of 2 - 1)

The value range of long is (- 922337203685474808 ~ 9223372036854774807), occupying 8 bytes (- 63rd power of 2 to 63rd power of 2 - 1)

The value range of float is 3.402823e + 38 ~ 1.401298e-45 (E + 38 represents the 38th power multiplied by 10, similarly, e-45 represents the negative 45th power multiplied by 10), occupying 4 bytes

The value range of double is 1.797693e + 308 ~ 4.9000000e-324, occupying 8 bytes

Whether integer or floating-point, their representation range and accuracy are limited, so sometimes large values need to be used when they can not meet the requirements or require high-precision calculation. Let's look at a chestnut first.

public class Test{static void main(String[] args) {double result = 2.0-1.1;
        System.out.println(result);
    }
}

Guess what?

Are you surprised? Are you surprised? You might think, WTF?!! Isn't it 0.9? In fact, due to the storage of floating-point numbers and the provisions of operation rules, the above results appear. In fact, floating point types cannot accurately represent the decimal part of 1.1. So why?

Ten thousand words are omitted here...

Such imprecision is not allowed when strict results are required. At this time, we need to use large values. Rewrite as follows:

import java.math.BigDecimal; main(String[] args) {
        BigDecimal result = BigDecimal.valueOf(2.0).subtract(BigDecimal.valueOf(1.1));
        System.out.println(result);
    }
}

Now we can get the results we want.

Large value operations are different from ordinary operations. Because large values in java do not overload operator operations and do not give programmers such power, they can only be calculated by calling methods.

+——add();

System. out. println(BigInteger.valueOf(10). add(BigInteger.valueOf(5)));

- ——subtract();

System. out. println(BigInteger.valueOf(10). subtract(BigInteger.valueOf(5)));

*——multiply();

System. out. println(BigInteger.valueOf(10). multiply(BigInteger.valueOf(5)));

/——divide(); System. out. println(BigInteger.valueOf(10). divide(BigInteger.valueOf(5)));

  %——mod(); Remainder

  System. out. println(BigInteger.valueOf(10). mod(BigInteger.valueOf(5)));

Comparison - compareto(); Greater than returns a positive number, less than returns a negative number, equal to 0

  System. out. println(BigInteger.valueOf(10). compareTo(BigInteger.valueOf(5)))

The static method valueof () is used to convert integer floating-point numbers to large numeric objects.

In fact, the content of large values is not much. After more practice, you can master them skillfully.

So far, the content of this article is over. Welcome to continue your attention!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>