Basic data Type box unboxing, with conversion method, BigDecimal operation, random random number

Source: Internet
Author: User
Tags reserved

There are eight basic data types, a wrapper class in 1.4 of the first eight data types, and a tool class. Provides a number of useful methods, after 1.4 changes, can be mixed with the basic data, so that the basic data types can also be transformed to the object, more appropriate everything is the object.


Before jdk1.4, the writing integer A=new integer (3), after 1.4, the writing integer b=3, that is, before 1.4, must have the new object and then pass the value, because 3 is an int type, Integer is a class, cannot be directly assigned value.


1.4 can be directly assigned because, in the write integer b=3, is converted to the integer b=integer.valueof (3), valueof to the int to Integer, in to B, so as before the actual wording, But make the operation more brief. This behavior is called boxing, and automatic conversion of basic data types is packaged as a data type encapsulation class.


Integer a=3 automatic boxing, int b=a;3 automatically boxed as Integer package class, A is an integer class, B is an int type, b how can refer to Integer Class A. There is also a method intvalue () in integer, and a is disassembled to type int,


when writing int b=3; compiling code, int b=a.intvalue (), and splitting a to basic data type is given to B reference, after 1.4, write int b=a, automatically intvalue () method, so this kind of automatic call dismantling way, called unboxing, Automatically disassemble the encapsulated class to the base data type.


parseint (); format string as encapsulated class, ToString (); Convert encapsulated class to toString ():


Writing a

String a= "1257";

Integer.parseint (a) will convert string A to encapsulated class 1257, and if no system is specified, the default is 10 to 10, and the integer is decimal.


Writing one parameter two

Integer.parseint (a,10), the latter 10 is to explain what a is the system, at this time the result of the format is still 1257, because 1257 is 10, in the conversion to 10 in the same system,


Integer.parseint (a,8), the default integer is 10, specifying a is 8, and 1257 The result of this octal conversion to Integer10 is 2351, the system cannot be randomly specified, 1257 is not more than 8, so you can specify 8. If the designation is binary, it is wrong. reasonably specified.


There is also a note in the specified transformation that cannot exceed the maximum value of this encapsulated class, such as Byte.parsebyte ("1257"); This is wrong, because the byte maximum is 127, exceeds the maximum, the error.


Integer.tostring (), converts an integer encapsulated class to a string, or specifies the integer.tostring (567,8), converts 10 in 567 to 8, and returns a string.


The other three converts the encapsulated class to a string, but ToString makes it easier to convert to a specified system.

integer.tobinarystring (1000); tobinarystring converts 1000 to a binary string.

integer.tohexstring (1000); tohexstring converts 1000 to 16-string

integer.tooctalstring (1000); tooctalstring converts 1000 to 8-string


Get some properties

System.out.println (integer.bytes); Gets the total number of bytes in an integer, one byte for eight bits
System.out.println (Integer.max_value); Get the maximum integer value
System.out.println (Integer.min_value); Get the integer minimum value
System.out.println (integer.size); How many digits do you get for an integer


Knowing its integer one, you can know the other 7 package types of methods, the remaining 7 package types have the method integer, the integer is eight of the most methods.


BigDecimal operation, API translation: immutable, arbitrary-precision signed decimal numbers.


When Folat and double are operating, there are always some problems and are extremely imprecise. The larger the number, the less accurate, the more the mantissa.

float e = 1.5f;
for (int i = 0; i < i++) {
E = (float) (e+ 0.1);
}
System.out.println (e);

The result of the final E is also the problem with 2.4999995,double. , it is now necessary to use BigDecimal to calculate the decimal number accurately.


BigDecimal d = new BigDecimal ("1.5");
for (int i = 0; i < i++) {
D = D.add (new BigDecimal ("0.1"));
}
System.out.println (D.tostring ());

The result is 2.5, the accuracy of the decimal has higher requirements, the need to use BigDecimal for accurate operation, but BigDecimal also exist one, speed is not so fast.


Why d = D.add (new BigDecimal ("0.1")); Call the addition operation why D also overwrite, in the previous Paste API translation, explained that BigDecimal is an immutable, arbitrary precision of the signed decimal number. He is an immutable, without overwriting a new quote, and finally printing D or 1.5.


In BigDecimal

Add () Addition

Subtract () subtraction

Multiply () multiplication

Divide () Division  
BigDecimal can not be an error,
New BigDecimal. Divide (new BigDecimal (3)); This is an error, you can specify to keep decimal places, specify the form of rounding.
New BigDecimal. Divide (new BigDecimal (3), 5,roundingmode.half_up); This 11/3 specifies that 5 decimal places be reserved and rounded up.
If you do not specify to keep a few decimal places directly to the upward position, if not specified rounding, encountered unlimited decimal on the error. If you do not specify both the carry and the reserved decimal, the decimal and the carry are not preserved, and the value is displayed directly. There is no error or decimal reservation problem.
Random random number
                Random a = new Random ();
		byte[] h = new BYTE[10];
		A.nextint (9);    Specify a random range, within 9 random value
		a.nextbytes (h);  Random values in byte range until filled with a byte array, only for byte. For


		(byte b:h) {
			System.out.print (b + "");
		}

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.