Java Learning Notes (math class, Arrays class, BigInteger class, BigDecimal Class)

Source: Internet
Author: User

Math class: Mathematical tools, mathematical calculations, root, logarithm, trigonometric functions, etc.

All methods are static methods, do not need to establish objects, directly called by the class name can be

Example:

Here to write a few in the daily development will be used, such as trigonometric functions usually do not use, understand can

 Packagedemo; Public classMathdemo { Public Static voidMain (string[] args) {function1 ();        Function2 ();        Function3 ();        Function4 ();    Function5 (); }     Public Static voidfunction1 () {inti = Math.Abs (-9);        System.out.println (i); //output: 9, Absolute    }     Public Static voidfunction2 () {DoubleD1 = Math.floor (6.5); DoubleD2 = Math.ceil (6.5); System.out.println (d1);//6.0System.out.println (D2);//7.0    }     Public Static voidFunction3 () {DoubleD1 = Math.pow (2.0, 3.0);        System.out.println (D1); //2 of the 3-time side, output: 8.0        DoubleD2 = MATH.SQRT (4.0);        System.out.println (D2); //4 Development, Output 2.0    }     Public Static voidFunction4 () { for(inti = 0; I < 10; i++) {            DoubleD =Math.random ();            System.out.println (d); //print random number between 0 and 1        }    }     Public Static voidfunction5 () {DoubleD = Math.Round (1.4234);        System.out.println (d); //output 1.0, get rounded value    }}

Arrays class: Array tool classes that help us do some things with arrays:

Some methods can save us a lot of code in daily development

Example:

 Packagedemo;Importjava.util.Arrays; Public classArraysdemo { Public Static voidMain (string[] args) {function1 ();        Function2 ();    Function3 (); }         Public Static voidfunction1 () {//Array in ascending order, here is a lot higher than the bubble sort performance of the fast row        int[] arr = {5,4,8,7,3,1,6};        Arrays.sort (arr);  for(inti = 0; i < arr.length; i++) {System.out.println (arr[i]); }        //Print in ascending order    }         Public Static voidfunction2 () {//array of binary search method, my 6th article has the principle of introduction, and the principle here is roughly the same        int[] arr = {11,4,5,7,9,13,1}; //the premise of dichotomy is an ordered arrayArrays.sort (arr); intindex = Arrays.binarysearch (arr, 7); SYSTEM.OUT.PRINTLN (index);//sorted after 7 of all is 3//Note here: If this element does not exist in the array//then return a negative number: (-insertion point-1)    }         Public Static voidFunction3 () {int[] arr = {1,2,3,4,5}; String String=arrays.tostring (arr);        System.out.println (string); //output: [1, 2, 3, 4, 5] string//The principle was written in my previous articles .    }}

Sometimes an array exceeds the size of a long type and needs to be computed.

BigInteger class: Large number operation

Example:

 Packagedemo;ImportJava.math.BigInteger; Public classBigintegerdemo { Public Static voidMain (string[] args) {function (); }     Public Static voidfunction () {BigInteger B1=NewBigInteger ("6666666666666666666666666666666"); BigInteger B2=NewBigInteger ("1234567891234567891234567891231123123"); BigInteger B3= B1.add (B2);//b1+b2SYSTEM.OUT.PRINTLN (B3);//1234574557901234557901234557897789789BigInteger b4 = b1.subtract (B2);//b1-b2SYSTEM.OUT.PRINTLN (B4);//-1234561224567901224567901224564456457BigInteger B5 = b1.multiply (B2);//b1*b2System.out.println (B5);//A very long number, which is no longer duplicated hereBigInteger B6 = b2.divide (B1);//B2/B1System.out.println (B6);//185185    }}

BigDecimal class: Floating-point large number operation, improve floating-point arithmetic precision

Example:

 Packagedemo;ImportJava.math.BigDecimal; Public classBigdecimaldemo { Public Static voidMain (string[] args) {System.out.println (0.09+0.01);//Output 0.99999System.out.println (1.0-0.32);//Output 0.67999//found that there would be inappropriate results here.//cause: In the computer binary, it means that the floating-point number is inaccurate.//Workaround: BigDecimal classfunction1 ();    Function2 (); }     Public Static voidfunction1 () {BigDecimal B1=NewBigDecimal ("0.09"); BigDecimal B2=NewBigDecimal ("0.01"); BigDecimal B3= B1.add (B2);//b1+b2SYSTEM.OUT.PRINTLN (B3);//get 0.10, rightBigDecimal B4=NewBigDecimal ("1.0"); BigDecimal b5=NewBigDecimal ("0.32"); BigDecimal b6= B4.subtract (B5);//b4-b5System.out.println (B6);//0.68, correctBigDecimal B7= B1.multiply (B2);//b1*b2SYSTEM.OUT.PRINTLN (B7);//0.0009, correct    }     Public Static voidfunction2 () {//There are some differences in division, and it is important to noteBigDecimal B1 =NewBigDecimal ("1.301"); BigDecimal B2=NewBigDecimal ("101"); //BigDecimal B3 = b1.divide (B2);//if it's not divisible, there'll be an exception .BigDecimal B3 = b1.divide (b2,2, BIGDECIMAL.ROUND_HALF_UP); //The second parameter is the meaning of preserving two decimal places//The third parameter is the retention mode: the nearest number rounding (that is, rounding retention mode), and many other modesSYSTEM.OUT.PRINTLN (B3);//keep two bits and get 0.02 results    }    }

Java Learning Notes (math class, Arrays class, BigInteger class, BigDecimal 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.