"Javase" Basic type wrapper class

Source: Internet
Author: User

************************************************************************* * * * Original:blog.csdn.net/clark_xu Xu Changliang's Column************************************************************************Basic type wrapper class

The 8 basic types of chalk for the Java language correspond to 8 wrapper classes, each of which encapsulates a corresponding basic type of member variable, and also provides some methods for using positive-to-correct data types

Java.lang.Interger

Int

Java.lang.Long

Long

Java.lang.Double

Double

Java.lang.Character

Char

Java.lang.Boolean

Boolean

Java.lang.Byte

Byte

Java.lang.Float

Float

Java.lang.Short

Short

3.1Compiler support for wrapper classes

before JDK 5 was released, the tedious " unboxing " and " boxing" operations were required when using wrapper class objects for operations

Packing

Interger i=integer.valueof (100);

Interger j=integer.valueof (200);

Unpacking + Boxing:

Integer k=interger.valueof (I.intvalue () + j.intvalue ());

JDK 5 adds automatic unpacking and boxing features, including

interger i=100;interger j=200;

Interger K=i+j;

3.2 IntegerAndDoubleCommon Methods for objects

The static constants of Integer and Double max_value and min_value are used to return

The parseint and parsedouble methods can parse the string into int and double data, respectively, and if the data string cannot be resolved to a numeric value will be thrown NumberFormatException, for example:

int N=integer. parseint ("123");

Double d=double.parsedouble ("123.456");

Integer tobinarystring and tohexstring methods convert int data to binary and hexadecimal string sequences, respectively

String bstr=integer.tobinarystring (100);

String hstr=integer.tohexstring (100);

3.3 BigDecimalClass

The floating data type of java float,double The rounding error when the operation is being computed, and if you want to get the exact result, use the Java.math.BigDecimal class

Use the constructor method of the String parameter, for example:

BigDecimal d1=new BigDecimal ("3.0");

BigDecimal object has subtract,add,multiply,divide method, which corresponds to divide method, specifies precision, prevents invalid decimal exception

BigDecimal d4=d1.divide (D2,8,BIGDECIMAL.ROUND_HALF_UP);

3.4 BigIntegerClass

Use BigInteger 's static method ValueOf to build the object, the number of arguments long type.

Created as:BigInteger sum=biginteger.valueof (1)

The number of bits obtained is:sum.toString.length ();

"Javase" Basic type wrapper class

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.