Java Basics Learning 12-common API-03

Source: Internet
Author: User
Tags arithmetic wrapper

Wrapper classes for basic data types

Basic data types such as int, float, double, Boolean, Char, and so on are not features of the object, such as: cannot invoke the method, the function is relatively simple. In order for the basic data type to have the characteristics of an object, Java provides a wrapper class for each base data type, which has the characteristics of the object.

Methods for converting strings to basic types

General format: wrapper class. parsexxxx (String str)

int A=integer.parseint ("123456"); System.out.println (a+1);

Converting a base type to a string

1, String a= basic data type + "";

2. Call the valueof (basic data type) method of string
3. Call the ToString (basic data type) method in the wrapper class

                int a = 2;        String B = A + "";        System.out.println (b + 1); String C = string.valueof (a); SYSTEM.OUT.PRINTLN (c + 1); String d = integer.tostring (a); System.out.println (d + 1);

Conversions between the base data type and the corresponding wrapper class object

Basic values-------> wrapper class Way Boxing operations

1. Construction method (basic data type)

2. Packing class. VALUEOF (basic data type) method

Integer i = new Integer (3); Boxing Operation manual System.out.println (i + 1); Integer II = new Integer ("3"); A numeric string must be passed in order to automatically convert the SYSTEM.OUT.PRINTLN (ii + 1) to an int type boxing operation; Integer i2 = integer.valueof (3); Boxing operation manual Integer ii2 = integer.valueof ("3"); Boxing Operations Manual

  Packaging------> Basic data Types Unpacking operations

Wrapper class object. Xxvalue (); Returns a basic data type

int a = I.intvalue (); Unpacking Operation manual System.out.println (a); Integer i = 4; Automatic boxing basic Data---> wrapper class int a = i + 5; Automatic unpacking Packaging class----> Basic Data System.out.println (a);

  Automatic box packing and unpacking

The base type can be evaluated directly using an operator, but the reference type is not available. and the basic type wrapper class as a reference type can be calculated,

The reason is that Java "Secretly" automatically transforms an object into a basic data type.

correspondingly, the value of the reference data type variable must be the memory space address value of new, and we can assign a value of a base type to a reference to a basic type wrapper class.

The reason is also that Java and "secretly" automatic conversion of the basic data type to the object.

Auto-Unpacking: objects are converted to basic values

Auto-Boxing: Basic values turn into objects

Automatic Boxing (byte constant pool ) details of the demo

when the value is within the byte range, it is automatically boxed, not the newly created object space but the original space is used.

Integer i1 = new Integer (+);    Manual boxing Integer i2 = new Integer (100); System.out.println (I1 = = i2);   FalseSystem.out.println (I1.equals (I2));      TrueSystem.out.println ("-------------------");//In the range of byte (-128-127), when you create a new reference, the same value will point to the same address. Integer x = 124;      Auto-boxing integer y = 124; SYSTEM.OUT.PRINTLN (x = = y);      TrueSystem.out.println (X.equals (y));     True

  System class

the System class cannot create objects manually because the constructor method is private and prevents objects from being created by the outside world. The System class is the static method, and the class name is accessed

Common methods

Math class

The Math class is a tool class that uses the class name directly when it is used. Method name is OK

Common methods

Arrays Class

Some methods of manipulating arrays are included in the arrays class, such as sorting, searching

Common methods

Sort method for sorting elements in the specified array (element values are sorted from small to large)

ToString method that returns the string form of the contents of the specified array element

The BinarySearch method, in the specified array, to find where the value of the given element appears. If no query is reached, the return position is-1. Requires that the array must be an ordered array.

Defines a method that receives an array that stores 10 student test scores, which require the return of the last three test scores with the lowest test score.  Package Com.oracle.demo1;import Java.util.arrays;public class Demo7 {public static void main (string[] args) {int[] score = {98,--------------] System.out.println (arrays.tostring (b)); public static int[] Print (int[] arr) {arrays.sort (arr);//Sort from small to large int[] A = new int[3]; System.arraycopy (arr,0,a,0,3);//The first three bits of the lowest score are copied to the new array A, return A;}}

  Big Data operations

BigInteger

integers longer than long in Java are not called integers, they are encapsulated as BigInteger objects. In the BigInteger class, implementing arithmetic is accomplished by means of methods, not by operators.

The construction method of BigInteger

Arithmetic code

public static void Main (string[] args) {//Big data encapsulated as BigInteger object          BigInteger big1 = new BigInteger ("12345678909876543210 ");          BigInteger big2 = new BigInteger ("98765432101234567890");          Add implements the addition operation          BigInteger bigadd = Big1.add (BIG2);          Subtract implementation subtraction Operation          BigInteger bigsub = big1.subtract (BIG2);          Multiply implements the multiplication operation          BigInteger Bigmul = big1.multiply (BIG2);          Divide implements the division operation          BigInteger bigdiv = big2.divide (BIG1);}

  BigDecimal class

The loss of precision is easily caused by the operation of float and double types. In this case, the BigDecimal class is used to calculate the computational accuracy.

Construction method

public static void Main (string[] args) {          //Big Data encapsulated as BigDecimal object          BigDecimal big1 = new BigDecimal ("0.09");          BigDecimal big2 = new BigDecimal ("0.01");          Add implements the addition operation          BigDecimal bigadd = Big1.add (BIG2);                    BigDecimal big3 = new BigDecimal ("1.0");          BigDecimal big4 = new BigDecimal ("0.32");          Subtract implementation subtraction Operation          BigDecimal bigsub = big3.subtract (BIG4);                    BigDecimal Big5 = new BigDecimal ("1.105");          BigDecimal big6 = new BigDecimal ("+");          Multiply implements the multiplication operation          BigDecimal Bigmul = big5.multiply (BIG6);

for the division operation of floating-point data , unlike integers , there may be infinite repeating decimal , Therefore, you need to retain and select the rounding mode for the desired number of digits

Java Basics Learning 12-common API-03

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.