CoreJava learning 3 -- & #8203; basic packaging class

Source: Internet
Author: User

Basic Package Type

The eight basic types of Java language correspond to eight "Packaging classes", each of which encapsulates a corresponding member variable of the basic type, it also provides some practical methods for this data type.

The packaging class allows the basic data to exist and use in the form of objects.

1. compiler support for packaging classes

Before JDK 5, the packaging class was used for Complicated "unpacking" and "Packing" operations: that is, the basic data type encapsulated by the packaging class object before the operation, after calculation, the result is encapsulated into a packaging object.

Integer I = Integer. valueOf (100 );

Integer I = Integer. valueOf (200 );

Integer k = Integer. valueOf (I. intValue () + j. intValue ());

JDK5 adds the function of automatic "unpacking" and "Packing", which is much simpler.

Integer I = 100;

Integer I = 200;

Integer k = I + j;

Automatic "unpacking" and "Packing" are the "preprocessing" operations of the compiler.


2. Common Integer and Double Methods

Integer. parseInt (obj): converts to int or double type, and throws NumberFormatException when parsing fails.

Integer. toBinaryString (int );

Integer. toHexString (int );

Converts an int value to a binary string or a hexadecimal string sequence.


3. BigDecimal class

Java floating point data float/double) There will be a rounding error during the operation, if you want to get accurate calculation results, you can use java. math. BigDecimal class.

BigDecimal d1 = new BigDecimal ("2.5 ");

BigDecimal d2 = new BigDecimal ("3.6 ");

System. out. println (d1.subtract (d2); // Subtraction

System. out. println (d1.add (d2); // Addition

System. out. println (d1.multiply (d2); // Multiplication

// Division

/* Note:

* Use the rounding principle to use division.

* Otherwise, when an infinite decimal point is encountered, it will always be exceeded, and if it goes infinitely, it will overflow.

* Use BigDecimal. ROUND_HALF_UP to rounding it.

*/

System. out. println (d1.divide (d2, BigDecimal. ROUND_HALF_UP ));

This class is used when we have strict requirements on computing accuracy, but double is sufficient in most cases.

4. BigInteger class

The storage range of integer types int and long provided by java is limited. This class can be used for larger integer operations, theoretically, the storage range of BigInteger is limited by the memory capacity.

There are also addition, subtraction, multiplication, division operations. It only applies to integer operations.

BigInteger sum = BigInteger. valueOf (1 );

For (int I = 2; I <= 200; I ++ ){

// The multiply parameter requires BigInteger, so I must be converted to BigInteger.

Sum = sum. multiply (BigInteger. valueOf (I ));

}

System. out. println (sum. toString (). length (); // The result length.

System. out. println (sum );


// How many light years are there?

Sum = sum. divide (BigInteger. valueOf (3600*24*360). multiply (BigInteger. valueOf (300000 )));

System. out. println (sum );


This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1294992

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.