zoukankan      html  css  js  c++  java
  • [常用类]Math、Random、System、BigInteger、BigDecimal

    Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象

    常用方法:
    int abs()    
    double ceil()       //向上取整
    double floor()      //向下取整
    int max(int a , int b)
    double pow(double a , double b)
    double random()
    int round(float a)  //四舍五入
    double sqrt(double a)        

    Random类的概述

    	* 此类用于产生随机数如果用相同的种子创建两个 Random 实例,
    	* 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
    * B:构造方法
    	* public Random()
    	* public Random(long seed)
    * C:成员方法
    	* public int nextInt()
    	* public int nextInt(int n)(重点掌握)

    * A:BigInteger的概述
    	* 可以让超过Integer范围内的数据进行运算
    * B:构造方法
    	* public BigInteger(String val)
    * C:成员方法
    	* public BigInteger add(BigInteger val)
    	* public BigInteger subtract(BigInteger val)
    	* public BigInteger multiply(BigInteger val)
    	* public BigInteger divide(BigInteger val)
    	* public BigInteger[] divideAndRemainder(BigInteger val)
     1 public class Demo4_BigInteger {
     2 
     3     /**
     4      ** A:BigInteger的概述
     5             * 可以让超过Integer范围内的数据进行运算
     6         * B:构造方法
     7             * public BigInteger(String val)
     8         * C:成员方法
     9             * public BigInteger add(BigInteger val)
    10             * public BigInteger subtract(BigInteger val)
    11             * public BigInteger multiply(BigInteger val)
    12             * public BigInteger divide(BigInteger val)
    13             * public BigInteger[] divideAndRemainder(BigInteger val)
    14      */
    15     public static void main(String[] args) {
    16         //long num = 123456789098765432123L;
    17         //String s = "123456789098765432123";
    18         
    19         BigInteger bi1 = new BigInteger("100");
    20         BigInteger bi2 = new BigInteger("2");
    21         
    22         System.out.println(bi1.add(bi2));             //+
    23         System.out.println(bi1.subtract(bi2));            //-
    24         System.out.println(bi1.multiply(bi2));             //*
    25         System.out.println(bi1.divide(bi2));                   ///(除)
    26         
    27         BigInteger[] arr = bi1.divideAndRemainder(bi2);    //取除数和余数 存到数组中
    28         
    29         for (int i = 0; i < arr.length; i++) {
    30             System.out.println(arr[i]);
    31         }
    32     }
    33 }
    
    
    * A:System类的概述
    	* System 类包含一些有用的类字段和方法。它不能被实例化。 
    * B:成员方法
    	* public static void gc()
    	* public static void exit(int status)
    	* public static long currentTimeMillis()
    	* pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
    * C:案例演示
    	* System类的成员方法使用
    
     1 public class Demo3_System {
     2 
     3     /**
     4      * * A:System类的概述
     5             * System 类包含一些有用的类字段和方法。它不能被实例化。 
     6         * B:成员方法
     7             * public static void gc()
     8             * public static void exit(int status)
     9             * public static long currentTimeMillis()
    10             * pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 
    11         * C:案例演示
    12             * System类的成员方法使用
    13      */
    14     public static void main(String[] args) {
    15         //demo1();
    16         //demo2();
    17         //demo3();
    18         
    19         int[] src = {11,22,33,44,55};
    20         int[] dest = new int[8];
    21         for (int i = 0; i < dest.length; i++) {
    22             System.out.println(dest[i]);
    23         }
    24         
    25         System.out.println("--------------------------");
    26         System.arraycopy(src, 0, dest, 0, src.length);        //将数组内容拷贝
    27         
    28         for (int i = 0; i < dest.length; i++) {
    29             System.out.println(dest[i]);
    30         }
    31     }
    32 
    33     public static void demo3() {
    34         long start = System.currentTimeMillis();        //1秒等于1000毫秒
    35         for(int i = 0; i < 1000; i++) {
    36             System.out.println("*");
    37         }
    38         long end = System.currentTimeMillis();            //获取当前时间的毫秒值
    39         
    40         System.out.println(end - start);
    41     }
    42 
    43     public static void demo2() {
    44         System.exit(1);                            //非0状态是异常终止,退出jvm
    45         System.out.println("11111111111");
    46     }
    47 
    48     public static void demo1() {
    49         for(int i = 0; i < 100; i++) {
    50             new Demo();
    51             System.gc();                        //运行垃圾回收器,相当于呼喊保洁阿姨
    52         }
    53     }
    54 
    55 }
    56 
    57 class Demo {                                    //在一个源文件中不允许定义两个用public修饰的类
    58 
    59     @Override
    60     public void finalize() {
    61         System.out.println("垃圾被清扫了");
    62     }                            
    63     
    64 }

    
    
    
    * A:BigDecimal的概述
    	* 由于在运算的时候,float类型和double很容易丢失精度,演示案例。
    	* 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
    
    	* 不可变的、任意精度的有符号十进制数。
    * B:构造方法
    	* public BigDecimal(String val)
    * C:成员方法
    	* public BigDecimal add(BigDecimal augend)
    	* public BigDecimal subtract(BigDecimal subtrahend)
    	* public BigDecimal multiply(BigDecimal multiplicand)
    	* public BigDecimal divide(BigDecimal divisor)
    * D:案例演示
    	* BigDecimal类的构造方法和成员方法使用
     1 import java.math.BigDecimal;
     2 
     3 public class Demo5_BigDecimal {
     4 
     5     /**
     6      * * A:BigDecimal的概述
     7             * 由于在运算的时候,float类型和double很容易丢失精度,演示案例。
     8             * 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
     9         
    10             * 不可变的、任意精度的有符号十进制数。
    11         * B:构造方法
    12             * public BigDecimal(String val)
    13         * C:成员方法
    14             * public BigDecimal add(BigDecimal augend)
    15             * public BigDecimal subtract(BigDecimal subtrahend)
    16             * public BigDecimal multiply(BigDecimal multiplicand)
    17             * public BigDecimal divide(BigDecimal divisor)
    18         * D:案例演示
    19             * BigDecimal类的构造方法和成员方法使用
    20         十进制表示1/3
    21         0.3333333333333333333333333333333333333333 
    22              */
    23     public static void main(String[] args) {
    24         //System.out.println(2.0 - 1.1);
    25         /*BigDecimal bd1 = new BigDecimal(2.0);        //这种方式在开发中不推荐,因为不够精确
    26         BigDecimal bd2 = new BigDecimal(1.1);
    27         
    28         System.out.println(bd1.subtract(bd2));*/
    29         
    30         /*BigDecimal bd1 = new BigDecimal("2.0");        //通过构造中传入字符串的方式,开发时推荐
    31         BigDecimal bd2 = new BigDecimal("1.1");
    32         
    33         System.out.println(bd1.subtract(bd2));*/
    34         
    35         BigDecimal bd1 = BigDecimal.valueOf(2.0);    //这种方式在开发中也是推荐的
    36         BigDecimal bd2 = BigDecimal.valueOf(1.1);
    37         
    38         System.out.println(bd1.subtract(bd2));
    39     }
    40 
    41 }



  • 相关阅读:
    创建自定义图标和图像
    使用_CRTDBG_LEAK_CHECK_DF检查VC程序的内存泄漏(转)
    Hadoop Netflix数据统计分析2(转)
    系统提供的按钮和图标
    STATIC变量问题
    表格视图,文本视图和Web视图
    Hadoop Netflix数据统计分析1(转)
    获取程序数据路径(转)
    C++中STRING转为INT (转)
    netflix 推荐算法学习1(转)
  • 原文地址:https://www.cnblogs.com/gaoyang666/p/11158221.html
Copyright © 2011-2022 走看看